Lightning speed WordPress with Lighttpd and Supercache, part II

There 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 gzip header, lightpress would serve the dynamic page (instead of the cached html page).

I did some refactoring and testing, and here’s the current version of rewrite.lua. This one will serve the correct gzipped or text content, while solving another bug with urls ending in ”/”:

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"])
 
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

12 Responses to “Lightning speed WordPress with Lighttpd and Supercache, part II”

  1. Lighttpd and WP-Supercache? Now you can! -- A Tempest of Thoughts Says:

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

  2. Rafal Piekarski Says:

    I will try your sollution but tell me on which version of Lighttpd did you try it? And what about the rewrite rules for WP-SuperCache like this from point 7: http://wordpress.org/extend/plugins/wp-super-cache/installation/ ?

  3. Giovanni Intini Says:

    I’m using lighttpd 1.4.18 on ubuntu. The rewrite rules on the installation doc are for apache. I ported the to lua for mod_magnet (lighttpd).

  4. Rafal Piekarski Says:

    And what about changing the wp-super-cache.php files to support the lighttpd configuration without apache and his .htaccess files?

  5. Giovanni Intini Says:

    You mean hacking wp supercache so it supports lighty? I will contact the author to see if we can arrange it, meanwhile this approach works flawlessly.

  6. A Tempest of Thoughts » Blog Archive » New tempe.st, new performance, new server Says:

    [...] I’ve been looking for a new theme for a long time, because I needed a wider content column, and I’ve also implemented the superfast lighttpd + wp-supercache setup I described in a previous post. [...]

  7. Avena Says:

    I’m trying to use this solution but even when the plugin does save a cache file within /cache dir the comment on the files doesn’t display the msg: “” so I guess it isn’t working. :(

    I copied your rewrite.lua and nice permalinks are working, so WP it totally using it.

  8. Avena Says:

    update: it works, it was a problem with permissions.

  9. Giovanni Intini Says:

    Avena, sorry I didn’t read you comment earlier, but I’m glad you made it work :)

  10. Metabloging | Web 2.0 Dude Says:

    [...] Danach habe ich das Plugin Supercache eingespielt. Hilfreich dabei war dieses Snippet von A Tempest of Thougts. [...]

  11. Tony Says:

    Does this work for lighttpd 1.5?

  12. Giovanni Intini Says:

    I used it on 1.4.18 if you try and it works please let me know.