nginx+WP super cache设置安装

一直想给blog加上wp super cache,但是由于nginx不支持.htaccess而本人也不懂nginx的rewrite rule,所以一直没有成功过。最近有空重新试了下,在n次修改conf之后终于让wp super cache跑起来了,所以记下来当作备忘。

首先需要确认nginx是否支持url rewrite,可以用nginx -V来检查编译参数。由于我是从源码编译的,所以不存在这个问题,不清楚二进制源的版本如何。如果带有如下参数就说明有rewrite支持:

--with-pcre

建议同时把--with-http_gzip_static_module加进编译参数里面。

确认之后就是修改nginx的配置文件。在location /里面加入一行:

include wp-supercache;

wp-supercache是我的配置文件的名字,可以随便取。

接下来是建立wp-supercache。这个里面包含了对permalink的支持,不需要另外的文件。我的blog是放在/blog里面,如果是根目录就把/blog/替换成/。

rewrite ^/blog/wp-admin/?$ /blog/wp-admin/index.php last;
rewrite ^/?$ /index.htm last;

if (-f $request_filename/blog/index.php){
 rewrite /blog/(.\*) $1/blog/index.php;
}

if (!-f $request_filename){
 rewrite ^/blog/(.+)$ /blog/index.php last;
}

set $supercache_file '';
set $supercache_uri $request_uri;

if ($request_method = POST) {
 set $supercache_uri '';
}

# Using pretty permalinks, so bypass the cache for any query string
 if ($query_string) {
 set $supercache_uri '';
}

if ($http_cookie ~\* "comment_author_|wordpress|wp-postpass_" ) {
 set $supercache_uri '';
}

# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
 set $supercache_file /blog/wp-content/cache/supercache/$http_host/$1index.html;
}

# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
 rewrite ^/blog/(.\*)$ $supercache_file break;
}

# all other requests go to WordPress
if (!-e $request_filename) {
 rewrite . blog/index.php last;
}

如果没有编译进http_gzip_static_module的话就需要把gzip_static on;注释掉,否则会出错。同时wp-supercache的设置也不能开compression。

调好之后先别重启nginx,先进blog后台改permalink,装好wp-super-cache,打开cache再重启nginx。

正常顺序重启nginx,设置permalink,安装激活插件就行。

一切顺利的话查看页面源文件在页面底部应该可以看到这样的信息:

<!-- Dynamic page generated in 0.206 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-20 00:35:22 -->
<!-- Compression = gzip -->

如果刷新了之后cache的时间没变的话就说明supercache起作用了。

本文参考了如下资料:

http://wordpress.org/support/topic/299528

http://blog.wapdevelop.net/under-the-pseudo-static-settings-wordpress-nginx-added/

http://ocaoimh.ie/wordpress-nginx-wp-super-cache/