网站Nginx开启FastCGI

  发布于 2019-09-25  896 次阅读


前言

Nginx 的 FastCGI是基于 Nginx 后端的缓存写入和读取缓存方式,将后端CGI服务返回的页面缓存起来,后续请求到来时直接返回页面,省去与后端服务通信及生成页面的消耗,与squid、varnish、CDN等原理类似,由于减少了后端请求,原则上性能会比WordPress的各种缓存插件高出许多,而且并不像缓存插件在 PHP 代码层面的读写权限面临很大的漏洞风险。

简单来说,开启FastCGI可以加快网站访问速度,减少服务器负担,而且它比缓存插件安全。

1. 添加模板

使用FastCGI缓存需要额外编译 ngx_cache_purge 模块。

可以查看这篇文章:

yum安装的nginx进行平滑升级及编译模块

2. 配置NGINX

以下内容添加到http{}中:

fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:256m inactive=1d max_size=1g;
#以下配置可以不变
fastcgi_temp_path /tmp/wpcachetemp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_hide_header Pragma; #不对从被代理服务器传来的应答进行转发
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

fastcgi_temp_pathfastcgi_cache_path其中设置的相应路径需要提前创建好。

  • fastcgi_cache_path 表示缓存存放目录。
  • fastcgi_temp_path 表示缓存临时文件存放目录。
  • levels 表示指定该缓存空间有两层 hash 目录,第一层目录为 1 个字母,第二层目录为 2 个字母,保存的文件名会类似/tmp/wpcache/c/29/XXXXXX ;
  • keys_zone 参数用来为这个缓存区起名,多站点需要使用不同keys_zone进行区分。
  • 256m 指内存缓存空间大小为 256MB。
  • inactive 的 1d 指如果缓存数据在 1 天内没有被访问,将被删除。相当于 expires 过期时间的配置。
  • max_size 的 1g 是指硬盘缓存空间为 1G。

完成了在 Nginx.conf 里上述代码的添加后,需要再在站点.conf 里添加缓存规则代码。

在server{}里面,建议放在站点 server_name 和 root 的下面为宜,不过我放在ssl下面也没出问题,具体如下:

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;
}
location ~ \.php$ {
{
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
    #新增的缓存规则,以上不需要修改
    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 304 1d;
}
#缓存清理配置(可选模块,请细看下文说明)
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; }
}

配置完成后,nginx -s reload ,如果一切正常的话打开F12->Network 选择名字与当前页面相同文件,如果没有文件按ctrl+R 。

在Headers->Response Headers中会出现X-cache 。

因为你是已登录的用户,X-cache后面是BYPASS 。切换浏览器或是清除cookie,然后再次查看X-cache,直到出现X-cache:HIT 才算是成功,没有出现可多刷新几次,如果一直不出现可能是哪里冲突了吧。

3. 清理缓存

可以使用WordPress缓存清理插件:Nginx Helper

也可以直接rm -rf /tmp/wpcache

或者 curl https://域名/purge/相应网址

参考网站:

开启 Nginx 的 FastCGI Cache 缓存,加速 WordPress 伪静态页面 By张戈博客

Nginx开启fastcgi_cache缓存加速,支持html伪静态页面 By明月登楼