Nginx配置http跳转https访问
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 }