阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

教你如何利用fastcgi_cache缓存加速WordPress

32次阅读
没有评论

共计 3007 个字符,预计需要花费 8 分钟才能阅读完成。

导读 WordPress 有很多的缓存加速方案,例如插件缓存(wp-super-cache、wp-rocket 等)、PHP 代码缓存等等,现分享本站使用的 nginx 缓存。利用 fastcgi_cache 缓存。

在使用 nginx 缓存之前,必须在 nginx 里面加载专门的模块,这个模块叫做 ngx_cache_purge。

添加 ngx_cache_purge 模块
下载 ngx_cache_purge 模块

ngx_cache_purge 模块的官方地址:http://labs.frickle.com/files/。在这个地址找到最新版的模块版本,使用 wget 下载。

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

我这里使用的就是 ngx_cache_purge-2.3。

编译安装 ngx_cache_purge 模块

使用 nginx - V 命令查看 nginx 是否已经安装了这个模块,如果没有安装,需要重新编译安装。

使用军哥 lnmp 一键安装包的同学,可以在 lnmp 的安装目录中找到 lnmp.conf 这个文件,然后在 nginx 模块中添加 ngx_cache_purge。之后重新平滑升级 nginx 即可。

修改 ngxin 配置

在使用 fastcgi_cache 缓存之前,必须先修改 nginx 配置,具体就是进入虚拟主机配置中,找到 domainname.conf,然后修改里面的 sever 配置。

# 下面 2 行的中的 wpcache 路径请自行提前创建,否则可能会路径不存在而无法启动 nginx,max_size 请根据分区大小自行设置
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
#忽略一切 nocache 申明,避免不缓存伪静态等
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#Ps:如果是多个站点,以上内容不要重复添加,否则会冲突,可以考虑将以上内容添加到 nginx.conf 里面,避免加了多次。server
    {
        listen 80;
        #请修改为自己的域名
        server_name zhangge.net;
        index index.html index.htm index.php default.html default.htm default.php;
        #请修改为自己网站的存放路径
        root  /home/wwwroot/domainname.com;
        set $skip_cache 0;
        #post 访问不缓存
        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;}
        #这里请参考你网站之前的配置,特别是 sock 的路径,弄错了就 502 了!location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #新增的缓存规则
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache WORDPRESS;
                fastcgi_cache_valid 200 301 302 1d;
        }
        location / {
                #此处可以添加自定义的伪静态规则(之前你新增的伪静态规则可以添加到这,没有就不用了)try_files $uri $uri/ /index.php?$args;
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
         }
        #缓存清理配置(可选模块,请细看下文说明)location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "此处填写你服务器的真实外网 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;}
        #请注意修改日志路径
        access_log /home/wwwlogs/domainname.com.log access;

注意修改上述代码中的该修改部分,不然 nginx 重启会出错。当然,如果是启用了 https,模块就应相应的改变。

安装 Nginx-helper 插件

在后台搜索 nginx-helper,安装好插件。

关于插件的设置:

如果没有使用 CDN 就可以选择 purge 模式,如果使用了 CDN 最好选择文件模式。

由于插件作者定义的缓存路径是 /var/run/nginx-cache,而我们可能会根据服务器实际情况来自定义缓存路径,这样一来,缓存路径的不同就会导致插件无法找到缓存文件并删除!

解决的方法:在 wp-config.php 中增加一行代码:

define('RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

这样,就配置好了。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-24发表,共计3007字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中