欢迎光临
我们一直在努力

Nginx-wordpress插件WP Super Cache的缓存规则

wordpress一个使用率非常高的博客系统,也可以把他说成是一个很好的php的cms系统,我们一般都叫他wp,是人都会有缺点,程序也不例外,虽然wp是一个非常好的程序,但他也在一些地方上存在缺陷,所以为了解决wp的一些缺陷,也就有好多人开发出好多好多的各类wp插件用来解决各中问题,WP Super Cache插件是款wp缓存插件,安装量已达100万+,他是一款非常棒的缓存插件,插件的缓存实现方式就有二种,一种是简单模式 (推荐),另一种是专家模式(专家缓存模式需要改变重要的服务器文件并且如果启用可能需要手动干预),今天我们就介绍下专家模式下nginx服务器的rewrite规则。

启用WP Super Cache的方式,是先在缓存功能选择项中选择启用缓存,然后在缓存实现方式中,选择专家模式,由于我的机器是nginx这里启用专家模式后,下面就会有提示,规则查阅中可以看到插件官方推荐的rewrite规则,以下是相关rewrite规则。

  • WP Super Cache Rules
# 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 ;
}    

官方推荐的rewrite规则,我试了下,不能缓存首页,对于移动端这里是全面的不缓存,所以推荐的规则好像并不太适合我们,最近这二年移动端很火,所以相关配置最好是对移动端友好一点比较利好我们,经过努力,在百度上面找到下面的rewrite配置规则,我感觉更适合我。

        location / {
		  if (-f $request_filename) {
		      break;
		  }
		  set $supercache_file '';
		  set $supercache_uri $request_uri;
		  set $supercache 1;
		  set $ihttp_host '';
		  if ($request_method = POST) {
		      set $supercache 0;
		  }
		  set $qs 0;
		  if ($query_string) {
		      set $qs 1;
		  }
		  if ($query_string ~* "^utm_source=([^&]+)&utm_medium([^&]+)&utm_campaign=([^&]+)(&utm_content=([^&]+))?$") {
		      set $qs 0;
		      set $supercache_uri $document_uri;
		  }
		  if ($qs = 1) {
		      set $supercache 0;
		  }
		  if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
		      set $supercache 0;
		  }
		  if ($http_user_agent ~* '(iphone|ipod|aspen|incognito|webmate|android|dream|cupcake|froyo|blackberry9500|blackberry9520|blackberry9530|blackberry9550|blackberry 9800|webos|s8000|bada)') {
		       set $ihttp_host '-mobile';
		  }
		  if ($supercache = 0) {
		       set $supercache_uri '';
		  }
		  if ($supercache_uri ~ ^(.+)$) {
		      set $supercache_file /wp-content/cache/supercache/$http_host$1/index${ihttp_host}.html;
		  }
		  if (-f $document_root$supercache_file) {
		      #rewrite ^(.*)$ $supercache_file break;
		      rewrite ^ $supercache_file last;
		  }
		  if (!-e $request_filename) {
		      rewrite . /index.php last;
		  }
        }

以上便是二个rewrite配置规则,我试了,全部可以,第一个rewrite规则不缓存首页和手机端,第二个rewrite规则不存在这个问题,那个rewrite规则合适用那个规则,有啥不明白的可以在下面留言。

赞(1) 打赏
原创文章转载请注明出处:爱编程 » Nginx-wordpress插件WP Super Cache的缓存规则
分享到: 更多

评论 抢沙发

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

爱编程、一个运维兼程序员的博客!

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏