nginx配置http(80)自动跳转到https(443)
nginx配置http(80)自动跳转到https(443)分两种情况,一是,单个域名80端口重定向到443端口,二是,泛域名的80端口重定向到443端口,如何启用https访问可参考网址:针对这两种情况分别进行介绍。
1、单域名配置
将访问单域名www.example.com的80端口重定向到443端口,只需在80端口配置中增加一条跳转命令:rewrite ^(.*)$ https://${server_name}$1 permanent; 即可实现访问单域名http自动跳转到https的功能,配置如下:
server {
listen 80;
server_name www.example.com;
rewrite ^(.*)$ https://${server_name}$1 permanent; #增加内容
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /opt/cert/www.example.com.pem;
ssl_certificate_key /opt/cert/www.example.com.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;
add_header Strict-Transport-Security "max-age=31536000";
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2、泛域名配置
将访问泛域名*.example.com的80端口重定向到443端口,只需在80端口配置中增加一条跳转命令:return 301 https://request_uri; 即可实现泛域名http自动跳转到https的功能,配置如下:
server {
listen 80;
server_name *.example.com;
return 301 https://$http_host$request_uri; #增加内容
}
server {
listen 443 ssl;
server_name *.example.com;
ssl_certificate /opt/cert/www.example.com.pem;
ssl_certificate_key /opt/cert/www.example.com.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;
add_header Strict-Transport-Security "max-age=31536000";
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
检查配置文件是否正确命令
nginx -t
然后重启nginx服务后,http自动跳转到https的配置即可生效了。
systemctl restart nginx
本文链接:
/archives/nginxautohttps
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
阳光•雨!
喜欢就支持一下吧