随着网络安全的提高,很多时候都用到了域名证书服务,比如商城,小程序,微端,app等,都需要后端提供https协议,现在分享几种http跳转https的方法,您可以根据实际情况选择!
方法一:nginx的rewrite方法
可以把所有的HTTP请求通过rewrite重写到HTTPS上,配置如下:
-格式一-
server{
listen 80;
server_name www.域名.com; //你的域名
rewrite ^(.*)$ https://www.域名.com permanent;
location ~ / {
index index.html index.php index.htm;
}
}
-格式二-
server{
listen 80;
server_name www.域名.com; //你的域名
return 301 https://$server_name$request_uri;
location ~ / {
index index.html index.php index.htm;
}
}
-格式三-
server{
listen 80;
server_name www.域名.com; //你的域名
rewrite ^(.*)$ https://$host$1 permanent;
location ~ / {
index index.html index.php index.htm;
}
}
方法二:meta刷新作用将http跳转到HTTPS
nginx配置:
server{
listen 80;
server_name www.域名.com; //你的域名
location ~ / {
root /vhost/www/;
index index.html index.php index.htm;
}
error_page 404 https://www.域名.com
}
方法三:nginx的497状态码
说明:497 – normal request was sent to HTTPS
当前站点只允许HTTPS访问,当使用HTTP访问nginx会报出497错误码
可以使用error_page 把497状态码链接重新定向到HTTPS域名上
nginx配置:
server{
listen 80;
server_name www.域名.com; //你的域名
error_page 497 https://$host$uri?$args;
location ~ / {
index index.html index.php index.htm;
}
}
方法很多,可根据您的环境自己选择方式方法。