Nginx强制http跳转https访问有以下几个方法
Nginx的rewrite方法
可以把所有的HTTP请求通过rewrite重写到HTTPS上
配置
方法一
1 server{ 2 listen 80; 3 server_name XXXXX.com; //你的域名 4 rewrite ^(.*)$ https://XXXXXX.com permanent; 5 location ~ / { 6 index index.html index.PHP index.htm; 7 } 8 }
方法二
1 server{ 2 listen 80; 3 server_name XXXXX.com; //你的域名 4 return 301 https://$server_name$request_uri; 5 location ~ / { 6 index index.html index.PHP index.htm; 7 } 8 }
方法三
1 server{ 2 listen 80; 3 server_name XXXXX.com; //你的域名 4 rewrite ^(.*)$ https://$host$1 permanent; 5 location ~ / { 6 index index.html index.PHP index.htm; 7 } 8 }
Nginx的497状态码
497 – normal request was sent to HTTPS
当前站点只允许HTTPS访问,当使用HTTP访问Nginx会报出497错误码
可以使用error_page 把497状态码链接重新定向到HTTPS域名上
1 server{ 2 listen 80; 3 server_name XXXXX.com; //你的域名 4 error_page 497 https://$host$uri?$args; 5 location ~ / { 6 index index.html index.PHP index.htm; 7 } 8 }
Meta刷新作用将http跳转到HTTPS
index.html
<html>
<Meta http-equiv=”refresh” content=”0;url=https://XXXX.com/”>
</html>
Nginx配置
1 server{ 2 listen 80; 3 server_name XXXXX.com; //你的域名 4 location ~ / { 5 root /var/www/test/; 6 index index.html index.PHP index.htm; 7 } 8 error_page 404 https://xxxx.com 9 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。