LNMP使用WP Super Cache插件Mod_Rewrite缓存模式

今天在折腾WP Super Cache插件的时候想使用mod_rewrite缓存模式,该模式的好处在于成功设置后博客可以实现静态化,就是把PHP停了网站也能运行的效果。mod_rewrite缓存模式在Apache下很简单,Nginx下的资料却很少,在linuxeye的网站上看到一篇文章测试了nginx规则可以使用,规则参考https://codex.wordpress.org/Nginx#WP_Super_Cache_Rules

首先修改nginx重写的规则:vim /usr/local/nginx/conf/wordpress.conf,清空后贴入如下代码

# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.

set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}

if ($query_string != "") {
set $cache_uri 'null cache';
}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}

# START MOBILE
# Mobile browsers section to server them non-cached version. COMMENTED by default as most modern wordpress themes including twenty-eleven are responsive. Uncomment config lines in this section if you want to use a plugin like WP-Touch
# if ($http_x_wap_profile) {
# set $cache_uri 'null cache';
#}

#if ($http_profile) {
# set $cache_uri 'null cache';
#}

#if ($http_user_agent ~* (2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800)) {
# set $cache_uri 'null cache';
#}

#if ($http_user_agent ~* (w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-)) {
# set $cache_uri 'null cache';
#}
#END MOBILE

# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
}

修改完使用service nginx reload,重载nginx配置。WP-Super-Cache高级设置的选项有几个必选的:
√ 启用缓存以便加快访问。 (推荐)
√ mod_rewrite 缓存模式。 (推荐)
√ 不要为已知用户缓存。 (推荐)
√ 缓存重建。当新缓存生成时调用缓存文件给匿名用户。 (推荐)
√Mobile device support.
(推荐)
√ 当某页面有新评论时,只刷新该页面的缓存。

其它选项看实际情况而定,选好后点击更新。在通用菜单里点测试缓存应该是OK的,然后我们打开网站查看源代码,

LNMP使用WP Super Cache插件Mod_Rewrite缓存模式

如上图所示,显示Cached page generated by WP-Super-Cache on就是缓存成功了。注意:要退出网站登录才行,因为上面已经勾选了不要为已知用户缓存,所以如果你是登录状态查看当然是不缓存的。

LNMP使用WP Super Cache插件Mod_Rewrite缓存模式

我们再来到内容菜单,点击重新生成缓存统计信息应该有些页面已经缓存了,再点击几个你熟悉的页面。然后我们在vps上service php-fpm stop关了PHP进程看看是不是网站依然能打开?刚才已经缓存过的页面打开也正常,这才是静态化的效果,不经过PHP网站也正常。好了,现在运行service php-fpm start把PHP重新开启吧。最后,到期时间和垃圾回收器根据你自己的实际需要设置吧。

weinxin
我的微信公众号
我的微信公众号扫一扫
广告也精彩
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: