/ Nginx

WordPress 开启 Fastcgi_cache 缓存

测试环境:

  • OneinStack(LNMP)
  • Centos6

安装Nginx ngx_cache_purge

1)检查ngx_cache_purge是否安装
<code class="language-null">nginx -V 2>&1 | grep -o ngx_cache_purge

显示 ngx_cache_purge 表示已经安装

2)安装ngx_cache_purge模块

OneinStack 下安装 ngx_cache_purge

<code class="language-c">cd /root/oneinstack/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
<code class="language-c"># 根据自己需要的nginx版本下载,查看Nginx版本命令:nginx -v
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xzf nginx-1.12.2.tar.gz
cd nginx-1.12.2
3)添加模块编译参数
<code class="language-c">nginx -V
#在返回的参数前加上./configure ,参数后加上--add-module=../ngx_cache_purge-2.3
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2n --with-pcre=../pcre-8.41 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3
#开始编译
make
4)备份nginx
<code class="language-c">mv /usr/local/nginx/sbin/nginx{,_`date +%F`} 
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 显示ngx_cache_purge表示已经安装成功

配置Nginx

1)部署server模块(修改nginx虚拟主机配置文件)

所有站点配置文件路径:/usr/local/nginx/conf/vhost/

修改 /usr/local/nginx/conf/vhost/domain.conf

添加两段代码块,从开始到结束:

<code class="language-c">server {
...
  index index.html index.htm index.php;
  root /data/wwwroot/domain.com;

   #——————————添加代码块1-fastcgi缓存配置【开始】——————————
  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;
      }
 #——————————添加代码块1-fastcgi缓存配置【结束】——————————

  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }

  include /usr/local/nginx/conf/rewrite/c.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
...

  location ~ [^/]\.php(/|$) {
    try_files $uri =404;
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
 #——————————添加代码块2-fastcgi缓存配置【开始】——————————
    #fastcgi缓存配置
    add_header X-Cache "$upstream_cache_status From $host";
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache domain.com;
    fastcgi_cache_valid  60m;
#——————————添加代码块2-fastcgi缓存配置【结束】——————————
  }
2)部署http模块(修改nginx配置文件)

Nginx文件路径:/usr/local/nginx/conf

添加一段代码块,从开始到结束:

<code class="language-c">user www www;
...
...
http {
...
  fastcgi_intercept_errors on;
 #——————————添加代码块1-fastcgi缓存配置【开始】——————————

#fastcgi站点1 注意修改域名
fastcgi_cache_path /dev/shm/domain_com_cache levels=1:2 keys_zone=domain.com:384m inactive=1d max_size=5G;

#fastcgi站点2 注意修改域名
fastcgi_cache_path /dev/shm/test_com_cache levels=1:2 keys_zone=test.com:384m inactive=1d max_size=5G;

#fastcgi站点3 如果需要,则继续添加更多站点
#fastcgi_cache_pat...

#fastcgi缓存配置(通用)
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

 #——————————添加代码块1-fastcgi缓存配置【结束】——————————
  #Gzip Compression
  gzip on;
...
3)Nginx验证规则并重启
<code class="language-null"># nginx -t && service nginx restart

安装 Nginx Helper 插件

WordPress后台【插件】—【安装插件】搜索【Nginx Helper】安装即可。如下设置:

修改wp-config.php

wordpress网站根目录wp-config.php添加:

<code class="language-null">define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/test_com_cache');

验证缓存

<code class="language-null">[root@ebs-23233 ~]# curl -I http://domain.com

返回

<code class="language-c">HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Feb 2018 16:24:11 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
#X-Cache带有此标记说明缓存成功,  HIT表示缓存命中,  MISS表示未命中,BYPASS表示缓存黑名单。
X-Cache: HIT From code.doocii.com

参考文章

WordPress 开启 Fastcgi_cache 缓存
Share this