Nginx强制http跳转https访问有以下几个方法
nginx的rewrite方法
可以把所有的HTTP请求通过rewrite重写到HTTPS上
配置
方法一
server{
listen ;
server_name XXXXX.com; //你的域名
rewrite ^(.*)$ https://XXXXXX.com permanent;
location ~ / {
index index.html index.php index.htm;
}
}
方法二
server{
listen ;
server_name XXXXX.com; //你的域名
return https://$server_name$request_uri;
location ~ / {
index index.html index.php index.htm;
}
}
方法三
server{
listen ;
server_name XXXXX.com; //你的域名
rewrite ^(.*)$ https://$host$1 permanent;
location ~ / {
index index.html index.php index.htm;
}
}
nginx的497状态码
497 – normal request was sent to HTTPS
当前站点只允许HTTPS访问,当使用HTTP访问nginx会报出497错误码
可以使用error_page 把497状态码链接重新定向到HTTPS域名上
server{
listen ;
server_name XXXXX.com; //你的域名
error_page https://$host$uri?$args;
location ~ / {
index index.html index.php index.htm;
}
}
meta刷新作用将http跳转到HTTPS
index.html
<html>
<meta http-equiv=”refresh” content=”0;url=https://XXXX.com/”>
</html>
nginx配置
server{
listen ;
server_name XXXXX.com; //你的域名
location ~ / {
root /var/www/test/;
index index.html index.php index.htm;
}
error_page https://xxxx.com
}