IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    WP Super Cache在Nginx下安装方法和URL重写(伪静态)规则设置

    小旭旭发表于 2015-12-18 16:26:06
    love 0

    WP Super Cache是目前缓存插件使用的最多的一个,在我的WordPress缓存优化中并未提起,但并不代表WP Super Cache不好。特别是nginx下面,其实是由于WP Super Cache在nginx安装使用,和URL重写即伪静态规则相对在apache下面更难设置。

    安装与设置WP Super Cache

    在WordPress仪表盘插件栏搜索WP Super Cche,安装即可。

    安装WP Super Cache
    安装WP Super Cache
    启用缓存功能 (推荐)
    启用缓存功能 (推荐)

    启用后,会出现提示:

    Mod rewrite 模块可能未安装!

    貌似 mod_rewrite 模块尚未安装。也许情况并不是这样,尤其是当您不使用 Apache 作为服务器程序的时候。请检查 mod_rewrite 模块是否被加载。这对于 Super Cache 的静态文件的调用是必需的。不过您可以使用 PHP 缓存模式或者传统模式运行本插件。

    不用管这个提示,这是针对apache服务器的提示,nginx下面不用管它。

    高级设置勾选项
    高级设置勾选项

    WP Super Cache的nginx url重写规则

    将原来的nginx重写规则

    try_files $uri $uri/ /index.php?$args;
    

    修改为

    try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
    

    在

    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
    

    千万不要插在

    location / {
        ....
    }
    

    中间。

    检验是否生效

    重启nginx,查看网页源码,底部是否增加了类似如下的注释,如果有即代表成功。

    <!-- Dynamic page generated in 0.421 seconds. -->
    <!-- Cached page generated by WP-Super-Cache on 2015-12-19 00:24:37 -->
    
    <!-- Compression = gzip -->
    

    我也承接各种WordPress优化,有需求的可以联系我。



沪ICP备19023445号-2号
友情链接