在Lighttpd上使用wp-super-cache
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的脚本文件,文件内容为:
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.st和thedomz.com,不过我对其中的第23行做了修改。原始版本永远不会对博客首页启用缓存,因为lua脚本仅在请求的原始地址不存在时才进行重定向操作,但首页也就是博客根目录总是存在的,因此也就永远不会被重定向到静态缓存。我对这一行的修改就是让脚本在请求的地址与站点根目录相同时也启用静态缓存重定向机制。
这样折腾完之后,调整一下站点的Permalink规则,使之不要掉进wp-super-cache的忽略规则里面,应该就可以了。在页面的html源代码最后,应该能够看到super-cache的时间戳。正常工作的wp-super-cache是不会在频繁刷新页面时每次都更新这个时间戳的。附带说一下,我这个博客是没有使用wp-super-cache的,本文提及的内容应用在了blog.laiyonghao.com。