要将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.test.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen 443; server_name www.test.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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论