How to Enable ETags and Compression in lighttpd

posted feb 4, 2013 at 12:00am

lighttpd is a great, low-fat webserver, but some of it's defaults make me want to pull my hair out. When Page Speed pointed out that I wasn't doing any caching or compressing on my static lighttpd install, it took me an hour of grubbing around in the docs to get mod_compress doing what I wanted, and ETags on all requests.

I'm writing down all the changes I had to make in the hopes of saving someone else one hour.

Compression

Who cares?

BetterExplained has a gentle introduction, but the tl:dr; is: trade some CPU cycles for smaller files, load pages faster. Plus, online website analyzers will stop making fun of you.

And it's off by default?

As of version 1.4.x, mod_compress is not in the default module set.

Open the network tab in the web developer tools of your favorite browser. Open a page on your site, notice that there is no Content-Encoding header. Note: curl doesn't ask for compressed content by default, so maybe for this tutorial curl is not going to be your favorite browser.

If you try to test for compression in curl, you are going to have a bad time.

Even worse, even if it was turned on, it wouldn't compress CSS or JS! Something about some dead browsers? I don't know about you, but my CSS and JS are waaaaay bigger than my HTML.

Alright, then how do I turn it on?

  1. In /etc/lighttpd/modules.conf, add mod_compress to the list of server.modules.
  2. Create a directory for storing compressed files:
    # mkdir -p /var/cache/lighttpd/compress
  3. In /etc/lighttpd/lighttpd.conf, add compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css"), so CSS and JS can join the compression party.
  4. Restart lighttpd.

You should now see Content-Encoding: gzip in the headers for HTML, JS, and CSS requests. Success!

ETags

Who cares?

Geez, always with the questions. Yahoo!?!1! has a great description, but the tl:dr; is: ETags let browsers ask if a page has changed before requesting the whole thing. These requests are much smaller and faster than getting the whole page, so it's another speed win. Again, the derision of programs made to judge website performance may be your main motivation here.

And it's off by default?

The feature is sparsely documented, but you can confirm by looking for an ETag header in that network tab we mentioned earlier. curl gets this one right, so curl -I example.com is an easy way to test whether ETags are working or not.

Alright, then how do I turn it on?

  1. In /etc/lighttpd/lighttpd.conf, add static-files = "enable"
  2. Restart lighttpd.

You should now see ETag: in the headers for your static file requests.