• 学派吧-由新云网络独家赞助-https://www.sq9.cn。

WordPress优化-利用Nginx fastcgi_cache缓存加速 | centos运维

unix admin 5年前 (2018-11-21) 3625次浏览 已收录 0个评论 扫描二维码

 

高并发网站架构的核心原则其实就一句话”把所有的用户访问请求都尽量往前推“,即:能缓存在用户电脑本地的,就不要让他去访问CDN。 能缓存CDN服务器上的,就不要让CDN去访问源(静态服务器)了。能访问静态服务器的,就不要去访问动态服务器。以此类推:能不访问数据库和存储就一定不要去访问数据库和存储。

WordPress最好的优化方式就是尽量不安装插件,之前有分享过《wordpress启动Redis缓存加速》,Wordpress是典型的PHP-MySQL应用,去做数据库缓存,倒不如让轻量级的Nginx直接去缓存WordPress内容。

Nginx内置FastCgi缓存,但是不支持自动清除缓存。当你在Wordpress里面新建/修改一篇文章,或者访客提交评论的时候,自动清空相关的缓存是必要的!Nginx需要安装ngx_cache_purg+量身定做的WordPress缓存清理插件:Nginx Helper。

 

1. 安装Nginx ngx_cache_purge模块

1)查看ngx_cache_purge是否安装

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

显示ngx_cache_purge表示已经安装

2)安装ngx_cache_purge模块

《OneinStack》和《lnmp一键安装包》下安装ngx_cache_purge模块

nbsp;

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`}  #备份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

修改nginx虚拟主机配置文件/usr/local/nginx/conf/vhost/blog.linuxeye.com.conf:

    1. fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;

 

    1. fastcgi_cache_key “$scheme$request_method$host$request_uri”;

 

    1. fastcgi_cache_use_stale error timeout invalid_header http_500;

 

    1. fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

 

 

    1. server {

 

    1. listen 443 ssl http2;

 

    1. ssl_certificate /usr/local/nginx/conf/vhost/linuxeye_blog.crt;

 

    1. ssl_certificate_key /usr/local/nginx/conf/vhost/linuxeye_blog.key;

 

    1. ssl_ciphers “CHACHA20:GCM:HIGH:!DH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS”;

 

    1. ssl_session_cache shared:SSL:10m;

 

    1. ssl_session_timeout 10m;

 

    1. #ssl_stapling on;

 

    1. #ssl_stapling_verify on;

 

    1. resolver 8.8.8.8 8.8.4.4 valid=300s;

 

    1. resolver_timeout 5s;

 

    1. server_name blog.linuxeye.com;

 

    1. access_log /home/wwwlogs/blog_nginx.log combined;

 

    1. index index.html index.htm index.php;

 

    1. include wordpress.conf;

 

    1. root /home/wwwroot/blog;

 

 

    1. set $skip_cache 0;

 

    1. if ($request_method = POST) {

 

    1. set $skip_cache 1;

 

    1. }

 

    1. if ($query_string != “”) {

 

    1. set $skip_cache 1;

 

    1. }

 

    1. if ($request_uri ~* “/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml”) {

 

    1. set $skip_cache 1;

 

    1. }

 

    1. if ($http_cookie ~* “comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in”) {

 

    1. set $skip_cache 1;

 

    1. }

 

 

    1. location ~ [^/]\.php(/|$) {

 

    1. #fastcgi_pass remote_php_ip:9000;

 

    1. fastcgi_pass unix:/dev/shm/php-cgi.sock;

 

    1. fastcgi_index index.php;

 

    1. include fastcgi.conf;

 

    1. fastcgi_cache_bypass $skip_cache;

 

    1. fastcgi_no_cache $skip_cache;

 

    1. fastcgi_cache WORDPRESS;

 

    1. fastcgi_cache_valid 60m;

 

    1. }

 

 

    1. location ~ /purge(/.*) {

 

    1. allow 127.0.0.1;

 

    1. deny all;

 

    1. fastcgi_cache_purge WORDPRESS “$scheme$request_method$host$1”;

 

    1. }

 

 

    1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {

 

    1. expires 30d;

 

    1. access_log off;

 

    1. }

 

    1. location ~ .*\.(js|css)?$ {

 

    1. expires 7d;

 

    1. access_log off;

 

    1. }

 

    1. }

 

使nginx配置生效

    1. service nginx reload

 

 

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

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

 

3. WordPress安装Nginx Helper插件

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

修改wordpress网站根目录wp-config.php添加如下行:

  1. define(‘RT_WP_NGINX_HELPER_CACHE_PATH’,’/dev/shm/nginx-cache’);

 

4. 测试

测试时候勾选Enable Logging(记录日志)、Enable Nginx Timestamp in HTML(插入缓存信息)

查看网页源代码:

查看Nginx Helper是否刷新日志:


学派吧 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress优化-利用Nginx fastcgi_cache缓存加速 | centos运维
喜欢 (2)
[pay@sq9.cn]
分享 (0)
关于作者:
腾讯云-运维运维 QQ 690624
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址