Wordpress开启https访问

首先去云服务器申请证书

这里购买免费的ssl证书,申请完成之后,直接在服务平台下载即可,放在如下的目录,这里参考阿里云的推荐配置,在nginx安装目录下的conf目录创建cert文件夹,将解压的证书放在cert文件夹下,因为我的nginx不仅仅配置了一个应用,都配置在conf的 vhost目录下
img
img

一定要注意位置,不然启动nginx会出现文件没有找到的错误

对wordpress文章内的图片及后台强制https设置。

wordpress上传的图片都写入了绝对地址,也就是启用https前所有上传到文章的图片都是以http://开头的地址存储,如果不转换成https访问网站时浏览器顶部的“小锁”会在某些浏览器显示安全警告,让图片变成https地址很简单,只需要在当前主题的functions.php文件中添加以下代码:

/* 替换图片链接为 https */
function my_content_manipulator($content){
    if( is_ssl() ){
        $content = str_replace('http://www.imisty.cn/wp-content/uploads', 'https://www.imisty.cn/wp-content/uploads', $content);
    }
    return $content;
}
add_filter('the_content', 'my_content_manipulator');

可自动将全站所有http地址图片自动转换成https地址图片。

开启登录和后台https访问。

  • 打开网站根目录下的wp-config.php文件,在文件底部添加:
/* 强制后台和登录使用 SSL */
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
  • 然后在后台管理界面设置-常规-wordpress地址和站点地址修改为https

  • 要确保网站的每个元素(如插件、JS、CSS文件、图片、内容分发网站等)都采用https协议:(可以在chromeF12看看有没有警报)

  • 修改“菜单”当中的所有“自定义链接”为 https;

  • 修改其他自己手贱写入的绝对链接的地址……

  • 替换文章所有的Http到https

直接修改数据库(注意先备份!注意先备份!!注意先备份!!!重要的事情说三遍!)

UPDATE wp_posts 
SET    post_content = ( Replace (post_content, 'http://www.imisty.cn/', 'https://www.imisty.cn/') );

但是实际操作的过程中我发现不需要使用这一步,毕竟有点危险,所以我将后台站点https设置完成之后,发现文章访问404,再修改为朴素链接之后直接顺带修改了数据库还是使用WordPress自带的修改的对我这样不懂PHP的小白还是很不错的

开始配置站点的配置文件 ,我的配置如下

网站这时候虽然支持了 HTTPS 访问,但是也可以使用 HTTP 来访问,考虑到搜索引擎目前收录的都是 HTTP 链接,需要将http重定向到https。

server {
        listen          80;
        server_name     www.imisty.cn imisty.cn;
        return 301 https://www.imisty.cn$request_uri;
        root            /var/www/html/wordpress;
        index           index.html index.htm index.php;
        client_max_body_size 100M;

        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php$1 last;
        }

        location ~ .*\.php(\/.*)*$ {
            include fastcgi.conf;
            fastcgi_index  index.php;
            fastcgi_pass  127.0.0.1:9000;

            fastcgi_connect_timeout 300;

            fastcgi_send_timeout 300;

            fastcgi_read_timeout 300;
        }


        error_log  logs/error_wordpress.log;
        access_log logs/misty.log combined;
    }
server {
 listen 443 ssl;
 server_name www.imisty.cn imisty.cn;
 root            /var/www/html/wordpress;
 index           index.html index.htm index.php;
 client_max_body_size 100M;

 ssl_certificate   cert/misty.pem;
 ssl_certificate_key  cert/misty.key;
 ssl_session_timeout 5m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;


 location ~ .*\.php(\/.*)*$ {
      include fastcgi.conf;
      fastcgi_index  index.php;
      fastcgi_pass  127.0.0.1:9000;

      fastcgi_connect_timeout 300;

      fastcgi_send_timeout 300;

      fastcgi_read_timeout 300;
     }

}

检验配置是否正确
/usr/local/nginx/sbin/nginx -t

Nginx安装http_ssl_module模块

Nginx如果未开启SSL模块,配置Https时提示错误

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx

因为在配置的时候已经安装过nginx,但是没有安装ssl模块

采用如下步骤安装nginx的ssl模块

  1. 进入nginx的ssl源码包
    cd ~/MyFille/nginx

  2. configure安装指定的模块
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

  3. make编译
    make
    4.一定不要执行make install,否则就覆盖安装了。

  4. 备份原有的nginx
    cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak

6.将刚刚编译好的nginx替换原来的,一定要先stop,不然会显示文件忙
cp ./objs/nginx /usr/local/nginx/sbin/

7.查看安装情况,如下信息说明正常安装

[root@izwz993bv9azta8nrfwuhqz sbin]# ./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

常见问题

启动nginx的时候出现如下错误

解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)

解决办法/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf使用nginx -c的参数指定nginx.conf文件的位置,注意这里的配置文件位置最好使用绝对路径

阿里云推荐nginx配置的警告信息

[warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
nginx 1.15 及以后的版本,不需要再写 ssl on; 了。

改为
应该把 ssl on 改成 listen 443 ssl 这样才对

主页和后台管理https访问正常,其他页面访问出现404错误

这个查阅资料发现时伪静态的问题,最后还是使用朴素链接解决这个问题,原因我也不想深究了

WP Editor.md 在修改为https访问之后卡加载界面

这个问题我在前端调试发现发现是js加载的时候使用了https需要将地址改为https的,最后我还是选择逃避式解决问题改用WP Githuber MD,另一个原因就是WP Editor.md作者不再维护了

小确幸

每一丝灵感都值得被记录,每一笔记录都是成长,每一点成长都值得欢呼

博主个人站: www.imisty.cn
CSDN博客: https://blog.csdn.net/lookinthefog
博客园 :https://imist.cnblogs.com/

希望能够认识一些热爱技术的小伙伴,欢迎友链接哟

posted @ 2019-08-27 11:58  iMisty  阅读(7336)  评论(0编辑  收藏  举报