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

存储架构 2016-06-07

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 ��


如何严格设置php中session过期时间 如何严格限制session在30分钟后过期! 1.设置客户端cookie的lifetime为30分钟; 2.设置session的最大存活周期也为30分钟; 3.为每个session值加入时间戳,然后在程序调用时进行判断; 至于为什么,我们首先来了解下php中session的基本原理:...
Why was the ArrayIterator subclass constructor nev... I'm baffled why a subclass to ArrayIterator is never getting its __construct method called. Consider this example: getIterator(); if ( ...
Sylius v1.0.7 发布,开源 PHP 电子商务网站框架... Sylius v1.0.7 发布了。Sylius 是一个开源的 PHP 电子商务网站框架,基于 Symfony 和 Doctrine 构建,为用户量身定制解决方案。可管理任意复杂的产品和分类,每个产品可以设置不同的税率,支持多种配送方法,集成 Omnipay 在线支付。 更新内容: ...
PHP regularly extracts parts from a string I've created a regular expression in C# but now I'm struggling when trying to run it in PHP. I presumed they'd work the same but obviously not. Does a...
PHP: rfc:default-session-strict-mode PHP RFC: Session strict mode default ini settings Version: 0.2 Date: 2018-02-13 Author: Pieter Hordijk, ...