WordPress速度優(yōu)化-Nginx fastcgi_cache緩存加速

高并發(fā)網(wǎng)站架構(gòu)的核心原則其實就一句話“把所有的用戶訪問請求都盡量往前推“,即:能緩存在用戶電腦本地的,就不要讓他去訪問CDN。 能緩存CDN服務器上的,就不要讓CDN去訪問源(靜態(tài)服務器)了。能訪問靜態(tài)服務器的,就不要去訪問動態(tài)服務器。以此類推:能不訪問數(shù)據(jù)庫和存儲就一定不要去訪問數(shù)據(jù)庫和存儲。
WordPress最好的優(yōu)化方式就是盡量不安裝插件,Wordpress是典型的PHP-MySQL應用,去做數(shù)據(jù)庫緩存,倒不如讓輕量級的Nginx直接去緩存WordPress內(nèi)容。
Nginx內(nèi)置FastCgi緩存,但是不支持自動清除緩存。當你在Wordpress里面新建/修改一篇文章,或者訪客提交評論的時候,自動清空相關的緩存是必要的!Nginx需要安裝ngx_cache_purg+量身定做的WordPress緩存清理插件:Nginx Helper。
1. 安裝Nginx ngx_cache_purge模塊
1)查看ngx_cache_purge是否安裝
nginx -V 2>&1 | grep -o ngx_cache_purge
顯示ngx_cache_purge表示已經(jīng)安裝
2)安裝ngx_cache_purge模塊
《OneinStack》和《lnmp一鍵安裝包》下安裝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編譯參數(shù),最后加上--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表示已經(jīng)安裝成功
2. Nginx配置
建議將fastcgi_cache_path設置tmpfs內(nèi)存中,操作系統(tǒng)不同tmpfs路徑也不同,如下:
CentOS:/dev/shm
Ubuntu和Debian:/run/shm
修改nginx虛擬主機配置文件/usr/local/nginx/conf/vhost/blog.linuxeye.com.conf:
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/linuxeye_blog.crt;
ssl_certificate_key /usr/local/nginx/conf/vhost/linuxeye_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/blog_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/blog;
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配置生效
service nginx reload
重啟系統(tǒng)后shm內(nèi)存中nginx-cache文件夾會丟失,為了使重啟生效(自動創(chuàng)建文件夾),修改/etc/init.d/nginx的make_dirs下一行添加:
[ ! -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網(wǎng)站根目錄wp-config.php添加如下行:
define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache');
4. 測試
測試時候勾選Enable Logging(記錄日志)、Enable Nginx Timestamp in HTML(插入緩存信息)
查看Nginx Helper是否刷新日志:

相關文章
CyberPanel安裝WordPress并配置偽靜態(tài)規(guī)則
下面教你如何在 CyberPanel安裝WordPress以及配置偽靜態(tài),需要的朋友可以參考下2023-12-27- 這篇文章主要介紹了wordpress無法安裝更新主題插件的解決辦法,需要的朋友可以參考下2020-12-27
WordPress必備數(shù)據(jù)庫SQL查詢語句整理
發(fā)現(xiàn)幾條比較實用的,適合 WordPress 實用的SQL語句。于是就趕緊收集分享出來了,需要的朋友可以參考下2017-09-23wordpress在安裝使用中出現(xiàn)404、403、500及502問題的分析與解決方法
wordpress是很多新手站長搭建個人博客最喜愛的程序,但是最近在使用WordPress的時候遇到了一些問題,所以想著將遇到問題總結(jié)分享出來,下面這篇文章主要給大家介紹了關于wo2017-08-11WordPress取消英文標點符號自動替換中文標點符號的優(yōu)雅方法
這篇文章主要介紹了WordPress取消英文標點符號自動替換中文標點符號的優(yōu)雅方法,需要的朋友可以參考下2017-04-04- 這篇文章主要給大家介紹了wordpress自定義上傳文件類型的方法,如WordPress默認允許上傳 .exe 后綴名的可運行文件,那么我們怎么禁止用戶在WordPress后臺發(fā)表文章時上傳 .e2016-12-19
- 大家可能發(fā)現(xiàn)了當實現(xiàn)了前端用戶中心,后臺控制面板就失去了作用,那么限制其他用戶進入后臺控制面板就很有必要了!那么我們要怎么做呢?通過下面這篇文章分享的方法后,只2016-12-19
WordPress實現(xiàn)回復文章評論后發(fā)送郵件通知的功能
這篇文章主要介紹了WordPress實現(xiàn)回復文章評論后發(fā)送郵件通知的功能,涉及wordpress針對評論與郵件的相關操作技巧,需要的朋友可以參考下2016-10-11WordPress使用自定義文章類型實現(xiàn)任意模板的方法
這篇文章主要介紹了WordPress使用自定義文章類型實現(xiàn)任意模板的方法,可通過自定義文章類型來實現(xiàn)任意模版的使用,具有一定參考借鑒價值,需要的朋友可以參考下2016-10-11WordPress后臺地址被改導致無法登陸后臺的簡單解決方法
這篇文章主要介紹了WordPress后臺地址被改導致無法登陸后臺的簡單解決方法,簡單分析了后臺無法登陸的原因與相應的解決方法,涉及針對wordpress配置項的簡單修改,需要的朋友2016-10-11