WordPress缓存优化记录

前言:

WordPress优化是根据网上相关教程去使用,我只是记录了,以留备用.

我的网站是宝塔为基础,所以我也只记录了宝塔面板如何操作

Redis缓存优化

使用Redis做数据库缓存,能够有效减少MySQL的查询次数

redis的部署这里就不记录,自行安装

WordPress配置文件修改:

编辑wp-config.php,找到define( 'DB_COLLATE', ''); 在下方添加如下内容:

define( 'WP_REDIS_CLIENT', 'credis' );
define('WP_REDIS_HOST', 'redis');
define('WP_REDIS_DATABASE', '0');

define("WP_REDIS_HOST', 'redis'); redis这里填写你的redis地址

安装Redis Object Cache插件

前面的都配置好了,那么Redis Object Cache打开即用

NGINX Fastcgi 缓存

使用 NGINX Fastcgi缓存 需要安装WordPress插件 Nginx-Helper

编辑对应网站配置文件,添加对应参数.

fastcgi_cache_key "$scheme$request_method$host$request_uri";

fastcgi_cache_path /dev/shm/fastcgi-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

备注:在几行代码是在server上方填写

创建缓存目录,并将网站内引用的PHP配置注释。

mkdir /dev/shm/fastcgi-cache -p

网站配置文件内注释掉以下:

#includeenable-php-74.conf

将下面参数内容添加到server字段内,添加到#SSL-END下面

set $skip_cache 0;
        if ($request_method = POST) {
            set $skip_cache 1;
        }   
        if ($query_string != "") {
            set $skip_cache 1;
        }   
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }   
       if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }
       location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass unix:/tmp/php-cgi-74.sock;
                fastcgi_index index.php;
                include fastcgi.conf;  
                add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache WORDPRESS;
                add_header Cache-Control  max-age=0;
                add_header Nginx-Cache "$upstream_cache_status";
                add_header Last-Modified $date_gmt;
                add_header X-Frame-Options SAMEORIGIN;
                add_header X-Content-Type-Options nosniff;
                add_header X-XSS-Protection "1; mode=block";
                etag  on;
                fastcgi_cache_valid 200 301 302 1d;
        }

      location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow xxx.xxx.xxx.xxx;    #更换服务器本机IP
            deny all;
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }
        
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\. { deny  all; access_log off; log_not_found off; }

wp的插件商店内安装 Nginx Helper插件然后配置,需要勾选Enable Purge保存启用。

wp-config.php配置文件加入下面代码

define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/fastcgi-cache');

访问测试是否有无缓存,退出网站登陆,游客访问,F12查看域名,是否有Nginx-Cache:HIT

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共2条

请登录后发表评论

    • 头像source0
    • 头像source0