Nginx如何配置将80端口重定向到443端口

要将 Nginx 配置为将 HTTP(80 端口)请求重定向到 HTTPS(443 端口),请按照以下步骤操作:
1. 创建或编辑 Nginx 配置文件
通常配置文件位于/etc/nginx/sites-available/,您需要修改或创建相应的配置文件(如default或您的站点配置)。
2. 配置 HTTP 重定向到 HTTPS
在配置文件中添加以下内容,将 HTTP 请求重定向到 HTTPS:
server {
listen 80;
listen [::]:80; # 支持 IPv6
server_name example.com www.example.com; # 替换为您的域名
# 强制重定向到 HTTPS
return 301 https://$host$request_uri;
}
3. 配置 HTTPS 服务器块
添加或修改 443 端口的配置,启用 SSL 并指定证书路径:
server {
listen 443 ssl;
listen [::]:443 ssl; # 支持 IPv6
server_name example.com www.example.com;
# SSL 证书配置
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # 替换为您的证书路径
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # 替换为私钥路径
# 推荐 SSL 配置(增强安全性)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# HSTS(可选但推荐)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# 网站根目录和其他配置
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
4. 检查防火墙设置
确保服务器防火墙允许 HTTP(80)和 HTTPS(443)端口:
sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw reload
5. 测试并应用配置
# 检查配置语法是否正确 sudo nginx -t # 重新加载 Nginx 使配置生效 sudo systemctl reload nginx
6. 验证配置
访问 http://example.com,应自动跳转到 https://example.com。
使用浏览器开发者工具检查网络请求,确认返回状态码为 301 或 307。
使用 SSL 检测工具(如SSL Labs)检查 HTTPS 配置安全性。
注意事项
- 证书路径:确保
ssl_certificate和ssl_certificate_key指向正确的证书和私钥文件(如 Let’s Encrypt 证书)。 - 多域名支持:若需处理多个域名,在
server_name中添加所有相关域名,或使用通配符*.example.com。 - IPv6 支持:若服务器启用 IPv6,需包含
listen [::]:80;和listen [::]:443 ssl;。 - HTTP/2:在 HTTPS 配置中添加
http2以启用 HTTP/2:listen 443 ssl http2; listen [::]:443 ssl http2;
错误处理:若遇到重定向循环,检查 SSL 配置是否正确,或暂时注释 HTTPS 配置,排查问题。
方法补充
nginx 80 端口重定向到 443 端口
nginx 80 端口重定向到 443 端口,也就是 http 访问自动跳转到 https。
配置如下:
1. 按照如下格式修改 nginx.conf 配置文件,80 端口会自动转给 443 端口,这样就强制使用 SSL 证书加密了,访问 http 的时候会自动跳转到 https 上面。
server {
server_name xxxx.com; # 域名
listen 80;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
ssl_certificate /etc/letsencrypt/live//fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live//privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live//chain.pem;
.... 其他配置信息
}
备注: ${server_name}可以换成$host
2. 重启 nginx
3. 示例(以下是我们生产的配置)
server {
listen 80;
server_name www.mybj123.com;
rewrite ^(.*)$ https://${server_name}$1 permanent; }
server {
listen 443;
server_name www.mybj123.com;
ssl on;
ssl_certificate /etc/pki/CA/certs/214321311540956.pem;
ssl_certificate_key /etc/pki/CA/certs/214321311540956.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
index index.php index.htm index.html;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php {
root /alidata/www/html;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf; set $path_info ""; set $fastcgi_script_name_new $fastcgi_script_name; if ($fastcgi_script_name ~* "^(.+\.php)(/.+)$" ) { set $fastcgi_script_name_new $1; set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name_new;
fastcgi_param SCRIPT_NAME $fastcgi_script_name_new;
fastcgi_param PATH_INFO $path_info;
}
location / {
root /alidata/www/html;
index index.php index.html index.htm; if (!-e $request_filename){
rewrite ^(.*)$ /index.php$1 last;
}
}
}
到此这篇关于使用 Nginx 如何配置将 80 端口重定向到 443 端口的全部内容了,更多相关 Nginx 端口 80 重定向 443 内容请搜索码云笔记以前的文章或继续浏览下面的相关文章希望大家以后多多支持码云笔记!
以上关于Nginx如何配置将80端口重定向到443端口的文章就介绍到这了,更多相关内容请搜索码云笔记以前的文章或继续浏览下面的相关文章,希望大家以后多多支持码云笔记。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 admin@mybj123.com 进行投诉反馈,一经查实,立即处理!
重要:如软件存在付费、会员、充值等,均属软件开发者或所属公司行为,与本站无关,网友需自行判断
码云笔记 » Nginx如何配置将80端口重定向到443端口

微信
支付宝