Nginx下使用WordPress的WP Super Cache插件

参考:http://codex.wordpress.org/Nginx#WP_Super_Cache_Rules

1. 在WordPress后台安装WP Super Cache插件并启用

进入后台在插件-安装菜单搜索WP Super Cache,这是wordpress官方推荐插件。

2. 修改WordPress的nginx配置文件

在server {}段中添加以下内容

# 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 ;
}

注意:以上内容含location / {}段,如果原先配置文件已有location / {}段则需合并内容。

3. 设置wp Super Cache插件

通用:启用缓存功能

高级:

    启用缓存功能以加快访问;
    选择Mod_Rewrite模式(无视警告Mod rewrite 模块可能未安装的警告);
    不要为 GET 请求缓存;
    缓存重建,当新缓存生成时调用缓存文件给匿名用户;
    Mobile device support(移动设备支持);
    当有新文章或页面的发布或更新时清除之前的缓存文件;
    当某页面有新评论时,只刷新该页面的缓存。

4. 检查缓存是否生效

打开网站页面,查看页面源代码最后是否有类似下面的信息:

<!-- Dynamic page generated in xxx seconds. -->
<!-- Cached page generated by WP-Super-Cache on yyy -->

Cached表示缓存成功,再等几分钟再刷新页面查看时间是否有变化,没变化表示读取原先的缓存(未重复生成)。

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 65 queries in 0.387 seconds