[SYSADMIN] Serve your WordPress cached pages with lighttpd and not PHP

Optimizing Your WordPress Cache Loads in Lighttpd.

If you don’t configure your wordpress virtual host properly in lighttpd, your wordpress cache will still make use of PHP.

Wouldn’t it be nice if all those cached requests were served directy from the webserver as the static files that they are, bypassing the CPU/memory load PHP can have, and use those resources for otherthings?

Install and Enable mod_magnet

For this to occur with lighttpd, you will need mod_magnet, so assuming you’re on a Ubuntu/Debian based linux distro let’s make sure we have it installed.

sudo apt-get install lighttpd-mod-magnet

Then let’s make sure it’s enabled, you can do this manually on your lighttpd.conf by adding “mod_magnet” to the list of enabled modules…

server.modules = (

or you can do it the lighty way:

sudo lighty-enable-mod magnet

(this simply makes a symlink to the 10-magnet.conf
file inside /etc/lighttpd/conf-enabled
which lighty will check upon startup)

The cache logic script that will be executed by lighttpd

Now, on your wordpress directory, create a file called rewrite.lua
and paste the following script in it:

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
return false

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
return false

if (lighty.env["uri.scheme"] == "http") then
ext = ".html"
ext = "-https.html"

cached_page = lighty.env["physical.doc-root"] .. "/wp-content/cache/supercache/" .. lighty.request["Host"] .. lighty.env["request.orig-uri"] .. "/index" .. ext
cached_page = string.gsub(cached_page, "//", "/")

attr = lighty.stat(cached_page)

if (attr) then
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.*") and not string.find(user_cookie,"wordpress_test_cookie")))
if (query_condition and cookie_condition) then
accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
if (string.find(accept_encoding, "gzip")) then
if not serve_gzip(cached_page) then serve_html(cached_page) end

Then on your vhost configuration in lighttpd.conf add the following towards the end.(Fix paths if you have to)

var.wp_blog = 1

magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )

url.rewrite-if-not-file = (
   "^/(wp-.+).*/?" => "$0",
   "^/(sitemap.xml)" => "$0",
   "^/(xmlrpc.php)" => "$0",
   "^/(.+)/?$" => "/index.php/$1"

Restart your lighttpd sudo service lighttpd restart

Now watch how your PHP processes breathe a lot better and you page loads are insanely faster.

You’re welcome ��

稿源:Gubatroncom (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » [SYSADMIN] Serve your WordPress cached pages with lighttpd and not PHP

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录


忘记密码 ?


Q Q 登 录
微 博 登 录