How to Enable ETags and Compression in lighttpd
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.
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?
-
In
/etc/lighttpd/modules.conf
, addmod_compress
to the list ofserver.modules
. -
Create a directory for storing compressed files:
# mkdir -p /var/cache/lighttpd/compress
-
In
/etc/lighttpd/lighttpd.conf
, addcompress.filetype = ("text/plain", "text/html", "text/javascript", "text/css")
, so CSS and JS can join the compression party. - 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?
-
In
/etc/lighttpd/lighttpd.conf
, addstatic-files = "enable"
- Restart lighttpd.
You should now see ETag:
in the headers for your static file
requests.