Lighttpd and WP-Supercache? Now you can!

Update: there are some bugs with this code. You can find the newest (and best) version here

It’s often said that if you can’t find something in Google it doesn’t exist, and when I couldn’t find a way to serve WP-Supercached pages via Lighttpd I was pretty sure no one had ever published on the net a solution to my problem.

Nice permalinks were easy to fix, thanks to this tutorial, but cache was a no-go: the lighttpd wiki and the wordpress forums had no info, so I had to cook a solution for myself.

It was easier than I thought though:

-- rewrite.lua, in your WP_ROOT
attr = lighty.stat(lighty.env["physical.path"])
 
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"
    if (string.find(accept_encoding, "gzip")) then
      if (lighty.stat(cached_page .. ".gz")) then
        lighty.header["Content-Encoding"] = "gzip"
        lighty.header["Content-Type"] = ""
        lighty.env["physical.path"] = cached_page
      end
    else
      if (lighty.stat(cached_page)) then
        lighty.env["physical.path"] = cached_page
      end
    end
  end
end

This rewrite.lua will serve compressed cached pages to those who accept gzip and plain html to those who prefer not to get gzip, while retaining the nice permalinks.

4 Responses to “Lighttpd and WP-Supercache? Now you can!”

  1. links for 2008-04-24 -- A Tempest of Thoughts Says:

    [...] Lighttpd and WP-Supercache? Now you can! mod_magnet and some lua to serve cached pages with lighttpd. (tags: lighttpd lua wordpress mod_magnet cache) [...]

  2. Richard Says:

    Can you help me out with the specific setup?

    I put your rewrite.lua in the root of my wordpress location, but I don’t know what exactly to put into lighttpd.conf so that it knows what to do with the lua, I pretty much copied from what your reference link referred to.

    The SuperCache plugin still says Mod_Rewrite isn’t configured, so it either doesn’t detect it or doesn’t recognise it because it isn’t Mod_Rewrite.

    For reference, these are the lighttpd.conf and rewrite.lua I’m using:
    http://rich90usa.homelinux.com/wordpress/lighttpd.conf
    http://rich90usa.homelinux.com/wordpress/rewrite.lua

  3. Giovanni Intini Says:

    SuperCache will keep saying mod_rewrite isn’t configured because it doesn’t know what we’re doing with our cached files. Nonetheless cache will be served. If you follow the nice permalinks tutorial and it works, just changing rewrite.lua to my rewrite.lua should enable cache for you.

  4. Lightning speed Wordpress with Lighttpd and Supercache, part II -- A Tempest of Thoughts Says:

    [...] was an error in the code I wrote for my previous post about lighttpd and wp-supercache. If you kept GZip disabled in you wp-supercache configuration, and your browser sent an Accepts [...]