Nginx以它的低系统资源消耗 、轻量级等特点深受喜爱,尤其是目前LNMP架构的服务器随处可见。WP Super Cache到目前仍然是停留在仅对Apache友好的状态……官方人员在09年时推出了使Super Cache在Nginx下可以正常生成静态网页(所谓的mod_rewrite)的方法,只可惜据现在已经有了将近5年的历史……WP酷在一番搜索后得出答案,现在与大家分享。
如果你使用的是军哥的LNMP一键包,那么直接替换/usr/local/nginx/conf/wordpress.conf文件的内容即可。
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; } }
注意set $supercache_file /wp-content/cache/supercache/$http_host$1/index${ihttp_host}.html; 必须与实际路径保持一致,如果不是,请自行修改。如果固定链接中含有index.php,请去掉。
回到WP Super Cache设置页面,勾选以下选项:
- mod_rewrite 缓存模式(必选)
- “缓存重建”和“移动设备支持”(可选)
- 当有新文章或页面的发布或更新时清除之前的缓存文件(可选)
- 当某页面有新评论时,只刷新该页面的缓存(必选)
其它以上未提及的选项一律取消!包括一些未提及到但官方推荐勾选的功能!将底部的垃圾回收期缓存超时时间更改为0秒:
至此WP Super Cache已可以正常运行,reload或者重启下VPS吧,如果你觉得那几个“Mod rewrite 模块可能未安装!”“重写规则必须被更新”过于碍眼的话,可以在主题文件functions.php中加入如下代码:
add_filter('got_rewrite', 'nginx_has_rewrites'); function nginx_has_rewrites() { return true; }
注意 set $supercache_file /wp-content/cache/supercache/$http_host$1/index${ihttp_host}.html; 必须与实际路径保持一致,如果不是,请自行修改。如果固定链接中含有index.php,请去掉。 不理解?请问是什么意思?谢谢
@粉丝 默认即可
@WP酷 还是不行,我就是军哥的lnmp1.1什么都没修改过
@WP酷 请问跟模板没有关系吧
@粉丝 有做过伪静态吗?没有是用不了的呢
我也用不了,求解决方法
http_user_agent 做手机识别那里需要加新的么?
感觉WSC后台给出的远比这个多啊。
谢谢博主,博客速度快了好多
博主,这个规则开启WP Super Cache后,首页加上index.php后缀仍然可以访问,正常情况下应该是301的
@宅 这儿还没注意……我过段时间看看
不错的插件,可惜跟移动主题冲突