使用Nginx fastcgi_cache缓存给wordpress加速

/ 0评 / 10

最近逛技术大牛博客比较勤,在张戈博客中发现Nginx fastcgi_cache,我像发现了新大陆一般,立马进入折腾状态,现已经给另一个站点成功部署!
简单的记录下部署过程
首先看看是否安装了ngx_cache_purge

nginx -V 2>&1 | grep -o ngx_cache_purge

如果显示ngx_cache_purge的话就说明你已经安装了。
没有的话老实点看下面步骤:
安装ngx_cache_purge,

cd /root/oneinstack/src
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 #查看nginx编译参数,最后加上--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`}
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 显示ngx_cache_purge表示已经安装成功!

2,配置nginx
修改nginx虚拟主机配置文件/usr/local/nginx/conf/vhost/www.onediarys.com.conf:
建议:将fastcgi_cache_path设置tmpfs内存中。(每个人用的系统都不一样)
在这里只能提供CentOS:/dev/shm

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 {
listen 443 ssl http2;
ssl_certificate /usr/local/nginx/conf/vhost/onediarys_blog.crt;
ssl_certificate_key /usr/local/nginx/conf/vhost/onediarys_blog.key;
ssl_ciphers "CHACHA20:GCM:HIGH:!DH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
#ssl_stapling on;
#ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
server_name blog.linuxeye.com;
access_log /home/wwwlogs/www_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/www;
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";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}

重启nginx使其生效
重启系统后shm内存中nginx-cache文件夹会丢失,为了使重启生效(自动创建文件夹),修改/etc/init.d/nginxmake_dirs下一行(大概52行)添加:

[ ! -d '/dev/shm/nginx-cache' ] && { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; }

安装Nginx Helper插件或者使用张戈Nginx-Helper纯代码原创版本
差点忘记了还要在网站根目录wp-config.php加上

define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache');

发表评论

电子邮件地址不会被公开。 必填项已用*标注