Nginx利用fastcgi_cache缓存php页面

前言

fastcgi_cache是一个nginx的插件,用于缓存fastcgi接口的执行结果,例如缓存php的执行结果。特别是php网站的首页与一些非交互页面,利用fastcgi_cache可以大幅度提升访问速度,并且降低php的执行压力。

配置

1. 在nginx的主配置文件

在主配置文件(nginx.conf)中添加缓存域

fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=cgi_wpcache:200m inactive=1d;

fastcgi_cache_path:缓存文件的路径,/dev/shm/为tmfs缓存文件系统,

实际储存在内存中,所以读写IO性能更高。
levels:缓存目录的结构层次,例如1:2,缓存文件会就生成在指定目录的再下两层目录中。
keys_zone:缓存域名称,在vhost内进行缓存时需要调用。
inactive:缓存不活动时间,若缓存内容在指定时间内未被访问将会被清理出缓存域。

2. 站点配置

location /archives/ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /data/webroot/$fastcgi_script_name;
include fastcgi_params;
  fastcgi_cache cgi_wpcache;
  fastcgi_cache_methods GET HEAD;
  fastcgi_cache_key $request_method$host$request_uri;
  fastcgi_cache_valid 200 2d;
  fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
  add_header X-Cache “$upstream_cache_status”;
}

fastcgi_cache:指定缓存域
fastcgi_cache_methods:指定缓存的请求方式
fastcgi_cache_key:指定缓存文件的标识,这个标识会MD5转码存储在缓存域的目录下
fastcgi_cache_valid:指定缓存状态,例如上文中只缓存响应状态码为200的请求所产生的返回页面两天
fastcgi_ignore_headers:默认情况下fastcgi_cache会忽略有特殊header的请求,并不进行缓存,官网说明。但当我们添加这个参数后,这些限制将不在存在。
add_header 将会在返回请求的response的header中添加一个X-Cache字段表示是否进行了缓存。如果需要也可以在nginx日志中通过log_format添加$upstream_cache_status字段

  • ·MISS 未命中,请求被传送到后端
  • ·HIT 缓存命中
  • ·EXPIRED 缓存已经过期请求被传送到后端
  • ·UPDATING 正在更新缓存,将使用旧的应答
  • ·STALE 后端将得到过期的应答

3.验证

配置完成后重新加载nginx后通过curl -I 访问如下:

[root@localhost local]# curl -I localhost/archives/1.php
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Mon, 25 Jun 2018 06:48:05 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.0.30
Expires: Tue, 26 Jun 2018 06:48:05 GMT
Cache-Control: max-age=86400
X-Cache: MISS

第一次访问 X-Cache: MISS 说明还未进行缓存。

[root@localhost local]# curl -I localhost/archives/1.php
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Mon, 25 Jun 2018 06:50:57 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.0.30
Expires: Tue, 26 Jun 2018 06:48:05 GMT
Cache-Control: max-age=86400
X-Cache: HIT

第二次访问发现X-Cache: HIT,表明/archives/1.php这个url已经被缓存,再下次访问nignx将不再会去请求php-fpm进行执行,而会直接从缓存中读取并返回,能够大大提高网站速度。

4. 遇到问题

a)访问空白
配置完毕后发现时常会出现页面访问空白的情况,再清空缓存目录后重新访问就恢复了。
经过测试发现是由于第一次使用HEAD方式访问,返回一个body为空的响应并被缓存了,所以之后再次访问就获取到了空白的页面。
这是由于fastcgi_cache_key中没有设置$request_method,将GET和HEAD的请求存储到了同一个key中。

b)页面缓存失败,一直MISS
默认情况下,请求的header中包含“Expires”, “Cache-Control”, “Set-Cookie”等,页面将不会被缓存,所以添加fastcgi_ignore_headers Cache-Control Expires Set-Cookie;即可。

利用nginx的fastcgi_cache缓存加速WordPress

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');

这样,就配置好了。