Nginx 环境强制 http 301 跳转 https 的方法

现在越来越多的人开始为网站启用 ssl 证书了,安装了 ssl 后我们都需要让 http 强制跳转 https,并确定网站唯一性,这里介绍了几种方法。

方法

提示:以军哥的 lnmp 一键安装包为例,Nginx 配置文件修改地址为/usr/local/nginx/conf/vhost/xx.com.conf
请输入图片描述
方法一:

if ($scheme = http ) {
return 301 https://$host$request_uri;
}

方法二:

server_name moerats.com ;
rewrite ^(.*) https://moerats.com$1 permanent;

方法三:

if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}

方法四:

server_name moerats.com ;
return 301 https://$server_name$request_uri;

最后输入/etc/init.d/nginx restart 重启 Nginx 即可

THE END