在Lighttpd上使用wp-super-cache

Posted by Elias on 五 11, 2009 in 个人网站的工具与思考 |

wp-super-cache是WordPress的一个静态缓存插件,对访问量大的博客或是在系统中使用了性能较差的插件时都是挺有用的。但这个玩意需要Apache的mod_rewrite来支持,lighty下可没有这玩意儿。所以需要咱们自己添加一个lua脚本,配合lighty的mod_magnet模块来代替mod_rewrite的功能。整个操作步骤是这样的:

lighty中博客站点的配置文件要这么写:

$HTTP["host"] == "blog.elias.cn" {
  server.document-root = "/home/public_html/elias"
  magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )
}

这里能用magnet.attract-physical-path-to的前提是mod_magnet已经安装并启用了,否则在lighty的error.log会报错。Debian里mod_magnet是单独的一个包,叫做lighttpd-mod-magnet,安装以后就可以把mod_magnet加入lighty的配置文件了。

然后再在博客站点的根目录新建一个名为rewrite.lua的脚本文件,文件内容为:

?Download rewrite.lua
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
37
38
39
40
41
42
43
function serve_html(cached_page)
  if (lighty.stat(cached_page)) then
    lighty.env["physical.path"] = cached_page
    print("Serving cached page: " .. cached_page)
    return true
  else
    return false
  end
end
 
function serve_gzip(cached_page)
  if (lighty.stat(cached_page .. ".gz")) then
    lighty.header["Content-Encoding"] = "gzip"
    lighty.header["Content-Type"] = ""
    lighty.env["physical.path"] = cached_page .. ".gz"
    print("Serving gzipped page: " .. cached_page .. ".gz")
    return true
  else
    return false
  end
end
 
attr = lighty.stat(lighty.env["physical.path"]) and lighty.env["physical.path"] ~= lighty.env["physical.doc-root"]
 
if (not attr) then
  lighty.env["uri.path"] = "/index.php"
  lighty.env["physical.rel-path"] = lighty.env["uri.path"]
  lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
 
  query_condition = not (lighty.env["uri.query"] and string.find(lighty.env["uri.query"], ".*s=.*"))
  user_cookie = lighty.request["Cookie"] or "no_cookie_here"
  cookie_condition = not (string.find(user_cookie, ".*comment_author.*") or string.find(user_cookie, ".*wordpress.*") or string.find(user_cookie, ".*wp-postpass_.*"))
  if (query_condition and cookie_condition) then
    accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
    cached_page = lighty.env["physical.doc-root"] .. "/wp-content/cache/supercache/" .. lighty.request["Host"] .. lighty.env["request.uri"] .. "/index.html"
    cached_page = string.gsub(cached_page, "//", "/")
    if (string.find(accept_encoding, "gzip")) then
      if not serve_gzip(cached_page) then serve_html(cached_page) end
    else
      serve_html(cached_page)
    end
  end
end

这个lua脚本源自tempe.stthedomz.com,不过我对其中的第23行做了修改。原始版本永远不会对博客首页启用缓存,因为lua脚本仅在请求的原始地址不存在时才进行重定向操作,但首页也就是博客根目录总是存在的,因此也就永远不会被重定向到静态缓存。我对这一行的修改就是让脚本在请求的地址与站点根目录相同时也启用静态缓存重定向机制。

这样折腾完之后,调整一下站点的Permalink规则,使之不要掉进wp-super-cache的忽略规则里面,应该就可以了。在页面的html源代码最后,应该能够看到super-cache的时间戳。正常工作的wp-super-cache是不会在频繁刷新页面时每次都更新这个时间戳的。附带说一下,我这个博客是没有使用wp-super-cache的,本文提及的内容应用在了blog.laiyonghao.com

相关日志

标签:,



5 Comments


七 11, 2009 at 4:55 下午

您好 我想问下

lighty中博客站点的配置文件要这么写: 这一句 是不是说的 wp-config.php 这个文件里面呢

还是那个 有点不明白?

可以帮我下吗?


 
Elias
七 15, 2009 at 12:22 上午

……不是,那句指的是lighttpd自己的配置文件。。我直觉上认为你并没在使用lighttpd作为服务器,所以我这篇很可能对你没啥用。


 
jyu..
七 28, 2009 at 4:10 下午

大人好~ 吾刚刚开始使用,English太多,完全不会用,看到你那强大无比,所以特来请教。 http://52gd8.cn/pmwiki

请问pmwiki可以开放注册?(因为想和朋友一起使用) 还有着么才可以像Wikipedia那样,搜索到没有就可以自动创建个新条目呢?

大人不知是否告知QQ呢?(MSN也行啦~)

PS:留言那里验证码完全不知的填什么,所以跑这来留言了。


 
Elias
八 23, 2009 at 6:39 下午

PmWiki的相关问题请给我写邮件探讨。


 
PTP
十一 29, 2009 at 4:19 下午

关于这个:http://www.elias.cn/ComUse/ThunderBird#toc15 的留言,不知道留哪里,所以到这里来了。

我看到你对thunderbird的使用介绍非常详细,想必你一定使用起来遇到过蛮多情况了。

我问下,我为了从foxmail搬迁到thunderbird,中间靠outlook express中转了一下,但TB从OE导入邮件的时候自动建立了一个OE文件夹,我把文件夹里面的邮件全部分别拖入收件箱和已发送邮件箱了,但OE文件夹我没法删除。这个怎么解决?

过几天,我得换台新电脑用了,thunderbird所有的个性化设置,附件,签名,及地址簿,是不是搬迁起来是挺容易的还挺麻烦的??有啥要注意的吗?


 

评论

Elias的邪异门 is proudly powered by WordPress.(京ICP备10013669号 瑞豪开源提供VPS)Theme design by Laptop Geek.
Copyright © 2010 All rights reserved. Entries (RSS) and Comments (RSS).