WordPress提供的免费和付费缓存加速插件还是有很多的,但是对于大部分插件给我们用户的感觉就是设置比较复杂,尤其是再遇到英文界面更加不懂如何设置。在这里,麦子有看到这款 Cachify 插件设置简单且支持DB, HDD, APC 和 Memcached多种缓存方式,如果我们有需要安装可以看看。
Cachify通过缓存文章、页面和自定义文章类型为静态内容来优化您的页面加载速度。我们可以从以下几个缓存方式中选择:数据库、服务器硬盘(HDD)、Memcached(仅在Nginx上可用)或 APC(Alternative PHP Cache)——直接在Web服务器的系统缓存中。用户访问时,页面或文章可直接从缓存中拉取。数据库和查询和PHP请求数可显著减少,如果选择了合适的缓存方式,这个数字可能为0。
插件地址:https://wordpress.org/plugins/cachify/
![图片[1]-WordPress安装Cachify轻便缓存插件加速网站速度-WordPress建站笔记](https://cos.cnwper.com/2022/04/Cachify-1.png)
我们可以直接激活下载。
![图片[2]-WordPress安装Cachify轻便缓存插件加速网站速度-WordPress建站笔记](https://cos.cnwper.com/2022/04/Cachify-2.png)
我们可以看到设置界面还很简单,而且还有中文语言。设置缓存方式有四种,比如我们需要用到 Memcached 缓存方式必须要Nginx环境引擎且我们当前服务器安装过 Memcached 才可以在下拉选择缓存方式可以看到。
我们需要在当前站点的nginx配置文件中加入:
## GZIP
gzip_static on;
## CHARSET
charset utf-8;
## INDEX LOCATION
location / {
error_page 404 405 = @nocache;
if ( $query_string ) {
return 405;
}
if ( $request_method = POST ) {
return 405;
}
if ( $request_uri ~ "/wp-" ) {
return 405;
}
if ( $http_cookie ~ (wp-postpass|wordpress_logged_in|comment_author)_ ) {
return 405;
}
default_type text/html;
add_header X-Powered-By Cachify;
set $memcached_key $host$uri;
memcached_pass localhost:11211;
}
## NOCACHE LOCATION
location @nocache {
try_files $uri $uri/ /index.php?$args;
}
然后我们在重启NGINX。如果我们是宝塔面板的话,直接用上面的替换掉WP的伪静态。
如果我们是Apache,一般是启动APC缓存方式,那我们在伪静态中也需要调整的。
# BEGIN CACHIFY
<IfModule mod_rewrite.c>
# ENGINE ON
RewriteEngine on
RewriteBase /
# set hostname directory
RewriteCond %{HTTPS} on
RewriteRule .* - [E=CACHIFY_HOST:https-%{HTTP_HOST}]
RewriteCond %{HTTPS} off
RewriteRule .* - [E=CACHIFY_HOST:%{HTTP_HOST}]
# set subdirectory
# sometimes %{REQUEST_URI} might be an empty string, so /$ won't match
RewriteCond %{REQUEST_URI} /$
RewriteRule .* - [E=CACHIFY_DIR:%{REQUEST_URI}]
RewriteCond %{REQUEST_URI} ^$
RewriteRule .* - [E=CACHIFY_DIR:/]
# gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:]
<IfModule mod_mime.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:.gz]
AddType text/html .gz
AddEncoding gzip .gz
</IfModule>
# Main Rules
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html -f
RewriteRule ^(.*) /wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
</IfModule>
# END CACHIFY
这是HTTPS方式,如果我们是HTTP,则需要这样。
# BEGINN CACHIFY
<IfModule mod_rewrite.c>
# ENGINE ON
RewriteEngine On
# GZIP FILE
<IfModule mod_mime.c>
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html.gz -f
RewriteRule ^(.*) /path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html.gz [L]
AddType text/html .gz
AddEncoding gzip .gz
</IfModule>
# HTML FILE
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html -f
RewriteRule ^(.*) /path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html [L]
</IfModule>
# END CACHIFY
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END