当我们的博客开始启用SSL证书之后我们就要考虑HTTP强制跳转到HPPTS的问题。笔者这里是使用的lnmp环境搭建的WordPress,以笔者的博客crowsong.xyz为例。
首先我们进入到Nginx的配置文件地址
cd /usr/local/nginx/conf/vhost
在这里可以看到一个以你自己域名命名的.conf文件。
打开修改该文件。
vim crowsong.xyz.conf
紧接着我们找到server中listen 80的这段。
有以下几种301跳转方法:
1、
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
2、注意修改域名
server_name crowsong.xyz ;
rewrite ^(.*) https://crowsong.xyz$1 permanent
3、
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}
4、注意修改域名
server_name crowsong.xyz ;
return 301 https://$server_name$request_uri;
笔者以使用的是第一种跳转的方法:

虽然我们设置完成了跳转,但是有时候还会被提示该网站不安全之类的提示。这主要是因为之前所使用的图片,CSS,JS等文件还是以HTTP连接存在的。所以我们需要将数据库中保存的内容也全部更改一下。
首先进入phpMyAdmin后台,之后选择你的数据库,在SQL中运行下面两条语句即可(注意修改成你的域名)
UPDATE wp_options SET option_value = replace( option_value, 'http://www.crowsong.xyz', 'https://www.crowsong.xyz' );
UPDATE wp_posts SET post_content = replace( post_content, 'http://www.crowsong.xyz/wp-content', 'https://www.crowsong.xyz/wp-content' );
之后执行即可。
参考资料:
- http://www.laozuo.org/9953.html
- https://lnmp.org/faq/lnmp-nginx-301-rewrite.html
- https://xuexb.com/post/nginx-url-rewrite.html#toc-9be
- http://www.fedora.hk/linux/fuwu/show_19.html
- https://blog.csdn.net/woshizhangliang999/article/details/51701327