Nginx下用cos-html-cache实现WordPress的真静态化

Gavin [wordpress]

2008.11.19

由于感觉自己的wordpress访问越来越慢,就狠下心来实现本博客的页面真静态化(之前已经通过Nginx的URL Rewrite实现了伪静态化)。在网上Google了一下,发现了cos-html-cache这个插件,口碑不错,就down下来装了。按照文档一路装下来,挺顺利的。现特记下来备忘。

一、安装步骤:

1、到这里下载插件。我安装的是当期最新版本:v2.6.1;

2、解压缩,确保文件夹里面没有二级目录,然后将其放置到插件目录下,插件目录通常是 `wp-content/plugins/`。Linux下则用命令:
$unzip cos-html-cache.zip

3、在wordpress后台对应的插件管理页激活该插件;

4、更改wordpress的Permalinks(永久链接)为非默认设置,我的是:/article/%post_id%.html;

5、确保gzip已经禁用,请到对应的页面检查设置:http://你wp的地址/wp-admin/options-reading.php(注:我的wordpress是2.6.3版本,没文档上的该设置);

6、在网站的根目录下创建一个文件 “index.bak”,并将属性设置成0666;

7、在网站的根目录下创建一个目录article(对应/article/%post_id%.html中的目录),并将文件夹属性设置成0777:
#chmod 0777 article/

8、安装完成。

当前端用户访问首页或者相应文章的url时,插件将会自动检测是否已经生成静态文件,如果没有,则于目录html/下生成相应的静态文件。静态文件会在文档末尾添加一句:

二、注意事项:

1、cos-html-cache只当前只能生成首页及文章的静态文件。如果不想首页生成静态文件,则可修改cos-html-cache.php(通过wordpress的后台可以edit)的
define(‘IS_INDEX’,true);// false = do not create home page cache

define(‘IS_INDEX’,false);// true = do create home page cache
则可。

2、我的Web服务器是Nginx,需要URL Rewrite,如果你也需要URL Rewrite,可以参考以下我的Nginx的虚拟机设置:

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
#
# www.laigw.name virtual host setting
#
server
{
	listen       80;
	server_name  www.laigw.name laigw.name;
	index index.htm index.html index.php;
	root  /srv/nginx/laigw;
 
	location / {
		if (-f $request_filename/index.html){
		        rewrite (.*) $1/index.html break;
		}
		if (-f $request_filename/index.php){
		        rewrite (.*) $1/index.php;
		}
		if (!-f $request_filename){
		        rewrite (.*) /index.php;
		}
	}
 
	location ~ .*\.php?$
	{
		include conf/fcgi.conf;      
		fastcgi_pass  127.0.0.1:10080;
		fastcgi_index index.php;
	}
 
	access_log  /var/log/nginx/access_laigw.log  access;
}

3、更新文档请参考官方文档