WordPress优化之利用Nginx fastcgi_cache缓存加速

Wordpress是典型的PHP-MySQL应用,去做数据库缓存,倒不如让轻量级的Nginx直接去缓存WordPress内容。Nginx内置FastCgi缓存,但

是不支持自动清除缓存。当你在Wordpress里面新建/修改一篇文章,或者访客提交评论的时候,自动清空相关的缓存是必要的!有一个量身定

做的WordPress缓存清理插件:Nginx Helper。

安装过程:

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.9.10.tar.gz

tar xzf ngx_cache_purge-2.3.tar.gz
tar xzf nginx-1.9.10.tar.gz
cd nginx-1.9.10

nginx -V --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-ipv6 --with-http_gzip_static_module --with-http_realip_module \
--with-http_flv_module --with-ld-opt=-ljemalloc \
--add-module=../ngx_cache_purge-2.3

make  #编译
mv /usr/local/nginx/sbin/nginx{,_`date +%F`}  #备份nginx
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 显示ngx_cache_purge表示已经安装成功


2. Nginx配置

建议将fastcgi_cache_path设置tmpfs内存中,操作系统不同tmpfs路径也不同,如下:

CentOS:/dev/shm

Ubuntu和Debian:/run/shm


server段顶部声明:

fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
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;


server段添加

server {

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(/|$) {
    #fastcgi_pass remote_php_ip:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid  60m;
    }
location ~ /purge(/.*) {
    allow 127.0.0.1;
    deny all;
    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }

}

使nginx配置生效  service nginx reload

3. WordPress安装Nginx Helper插件

WordPress后台【插件】—【安装插件】搜索【Nginx Helper】安装即可






你可能感兴趣的:(WordPress优化之利用Nginx fastcgi_cache缓存加速)