在 /etc/nginx/ 下创建 wordpress_params_regular 文件,把以下内容拷过去

1
2
3
4
5
6
7
8
9
10
11
12
# WordPress pretty URLs
if (-f $request_filename) {
expires max;
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php?q=$1 last;

# Enable nice permalinks for WordPress
error_page 404 = //index.php?q=$uri;

在 /etc/nginx/ 下创建 wordpress_params_supercache 文件,把以下内容拷过去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# if the requested file exists, return it immediately
if (-f $request_filename) {
expires 30d;
break;
}

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_wordpresswp-postpass_" ) {
set $supercache_uri '';
}

# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /var/www/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 ^(.*)$ $supercache_file break;
}

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

将这两部分rewrite规则include进虚拟主机配置文件中location php段,如果直接放到了location /段下,则会出现无法解析php文件的错误,具体表现为点首页或者任意其他php文件,都会出现下载,打开下载下来的文件,内容就是php代码

1
2
3
4
5
6
7
8
9
10
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
**include /etc/nginx/wordpress_params_regular;
include /etc/nginx/wordpress_params_supercache;**
}

重新启动 Nginx

1
# /etc/init.d/nginx restart

参考:http://www.vpsee.com/2009/06/run-wordpress-wpsupercache-with-nginx-fastcgi/