• 使用WP-Super-Cache 帮你加速构建在IIS7上的WordPress博客

    Date: 2009.04.26 | Category: WordPress | Tags: ,

    自从我将自己的博客搬家到海外的一个IIS7的服务器上,我发现速度比原来构建在Linux上的系统要严重慢很多。我也一直在尝试着寻找为什么WPIIS上跑竟然如此之慢,也想改变这种状况。

    将所有的日志和页面转换成静态html格式不失为一种加速博客很好的办法,通常我们都为我们的WordPress博客加装“WP-Super-Cache”插件。但很不幸,我在自己的主机上一直没用配置这个插件成功,总是遇到一些问题。不过今天我解决了问题,也在此和朋友们分享解决的经验。

    如果你是一个blogger,并且你的博客系统是基于WordPress加上IIS7,那么我的这篇文章一定会对你有所帮助的。在解决了那些非常麻烦的问题后,我终于成功的在我的博客上加装了WP-Super-Cache插件。让我们用这个插件来加速我们的博客吧!

    具体怎么做呢?来看看我的步骤。

    安装和配置部分

    通过10步就可以完成配置:

    1. 下载这个插件(这里就不多废话了,后台有插件搜索的地方,直接下载即可)

    2. 将插件解压到 /wp-content/plugins/wp-super-cache/ 目录下

    3. 将 /wp-content/plugins/wp-super-cache/wp-cache-phase1.php 文件复制到
    /wp-content/advanced-cache.php

    4. 打开 /wp-content/plugins/wp-super-cache/wp-cache.php 文件,找到如下内容:
    [sou­r­ce­code language=“php”]function wp_cache_check_link() { glo­bal $wp_cache_link, $wp_cache_file;[/sourcecode]
    将它们替换成 [sou­r­ce­code language=“php”]function wp_cache_check_link() { glo­bal $wp_cache_link, $wp_cache_file; if ( file_exists($wp_cache_link) ) return true; else { echo “advanced-cache.php does not exist”; echo “Create it by copying $wp_cache_file to $wp_cache_link on your server”; return false; } [/sourcecode]

    5. 将如下的片段加入到根目录下的web.config文件中。需要注意的是要保证WP Super Cache这个规则要优先于WordPress rule for pre­tty permalinks规则。这样才能让缓存生效。

    <rew­rite>
    <rules>
    <rule name=“WP Super Cache” stopProcessing=“true”>
    <match url=”^(\d{4})/(\d{2})/(.+?)/?$” ignoreCase=“false” />
    <con­di­tions>
    <add input=”{REQUEST_METHOD}” negate=“true” pattern=“POST” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true” pattern=”.*=.*” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true“
    pattern=”.*attachment_id=.*” ignoreCase=“false” />
    <add input=”{HTTP_COOKIE}” negate=“true“
    pattern=”^.*(comment_author_|wordpress|wp-postpass_).*$” ignoreCase=“false” />
    <add
    input=”{DOCUMENT_ROOT}\wp-content\cache\supercache\{HTTP_HOST}\{R:1}\{R:2}\{R:3}\index.html“
    matchType=“IsFile” />
    </conditions>
    <action type=“Rewrite“
    url=“wp-content/cache/supercache/{HTTP_HOST}/{R:1}/{R:2}/{R:3}/index.html” />
    </rule>

    <rule name=“Wordpress Pre­tty Per­ma­links” patternSyntax=“Wildcard”>
    <match url=”*” />
    <con­di­tions>
    <add input=”{REQUEST_FILENAME}” matchType=“IsFile” negate=“true” />
    <add input=”{REQUEST_FILENAME}” matchType=“IsDirectory” negate=“true” />
    </conditions>
    <action type=“Rewrite” url=“index.php” />
    </rule>
    </rules>
    </rewrite>

    值得注意的一点是,上述配置方法适用于那种年月日期的那种URL持久格式,比如:http://wordpress/index.php/2008/12/sample-post/这种样子。其它样子的需要另作调整。

    像本博客(http://www.PriorMind.com/)这样的情况:“/%post_id%.html”,经过我的实验,应该按照下面的方法配置:

    <rule name=“WP Super Cache” stopProcessing=“true”>
    <match url=”^([0–9]+).html$” ignoreCase=“false” />
    <con­di­tions>
    <add input=”{REQUEST_METHOD}” negate=“true” pattern=“POST” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true” pattern=”.*=.*” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true“
    pattern=”.*attachment_id=.*” ignoreCase=“false” />
    <add input=”{HTTP_COOKIE}” negate=“true“
    pattern=”^.*(comment_author_|wordpress|wp-postpass_).*$” ignoreCase=“false” />
    <add
    input=”{DOCUMENT_ROOT}\wp-content\cache\supercache\{HTTP_HOST}\{R:1}.html\index.html“
    matchType=“IsFile” />
    </conditions>
    <action type=“Rewrite” url=“wp-content/cache/supercache/{HTTP_HOST}/{R:1}.html/index.html” />
    </rule>

    此外,如果你像我一样也想把首页(就是那个index.php)给静态化了,那就再在规则最上面加上一个规则吧:

    <rule name=“WP Super Cache Index Page” stopProcessing=“true”>
    <match url=“index.php” ignoreCase=“false” />
    <con­di­tions>
    <add input=”{REQUEST_METHOD}” negate=“true” pattern=“POST” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true” pattern=”.*=.*” ignoreCase=“false” />
    <add input=”{QUERY_STRING}” negate=“true“
    pattern=”.*attachment_id=.*” ignoreCase=“false” />
    <add input=”{HTTP_COOKIE}” negate=“true“
    pattern=”^.*(comment_author_|wordpress|wp-postpass_).*$” ignoreCase=“false” />
    <add
    input=”{DOCUMENT_ROOT}\wp-content\cache\supercache\{HTTP_HOST}\index.html“
    matchType=“IsFile” />
    </conditions>
    <action type=“Rewrite” url=“wp-content/cache/supercache/{HTTP_HOST}/index.html” />
    </rule>

    6. 在根目录的wp-config.php文件里加上这么一行:

    define( ‘WP_CACHE’, true );

    不过其实一般情况下这行都是写好了的,呵呵。

    7. 登入你的后台,然后激活wp-super-cache这个插件

    8. 进入wp-super-cache插件,打开缓存的功能

    9. 最后还有一点,就是别忘了关掉你的关于GZip输出方面的插件,这样会造成wp-super-cache的错误。

    把这些都完成后,咱们就可以登陆到博客试试看了。速度应该会有所提升。如果希望更清晰的看到效果,那么就打开源文件,看看最后一行,是不是出现了这么一行字:

    <!– Dyna­mic Page Served (once) in x.xxx seconds –>

    如果有这行字,那么就恭喜你,配置成功了!

    让这个插件真正的为我们工作

    但是某些情况下,我们配置完成上述过程后,插件并不起作用。这到底是怎么回事呢?我就为此苦恼了很久。你也许会看到如下的信息:

    <!– Page not cached by WP Super Cache. No clo­sing HTML tag. Check your theme. –>

    但不必担心,这不是我们主题模板的问题,虽然它那么说。它的意思是我们没有闭合html标签,但实际上大多数情况不是这样的。这应该是由于你的某些插件和它有冲突造成的。StatPressCN和GZIP Output这两个插件大家要小心一些,会对cache造成一些影响,尽量把这两个插件禁用掉。找到了冲突的插件后,应该就会出现上面成功的那段信息了。

    如果你的博客比较慢的话,建议你尝试一下,会有非常明显的效果的。如果要问有多明显,来我的网站看看就清楚喽!

    –Paul

    参考文章:

    用中文写的,就再啰嗦两句,谈谈WP-Super-Cache的工作机理吧。其实这个插件主要是靠我们的URL重写机制来完成加速的。我们第一次访问到一个网页后,首先wp-super-cache会将所有内容生成一个静态网页,存放在“/wp-content/cache/supercache/你的域名”目录下。其后访问的时候,都会先检测是否有这么一个文件已经生成。如果已经有,那么直接输出这个html文件,就不进行运算了;如果没有,那么就像第一次那样生成一个输出并保存。大概就是这样一个过程,这也就是为什么wp-super-cache那个规则要限于permalink的原因,因为后者是通用规则,而前者是为了在筛选了符合规则的网页后直接指向静态文件的URL重写。

    但愿这篇文章能够帮助和我一样在IIS主机上架构WordPress的朋友们。配置这个确实有点繁琐,终于成功了,速度快了很多!

    • Share/Bookmark