Optimizing lighttpd with YSlow
Yahoo may trail Google where it counts, but they’ve been very cool about sharing information on optimizing their site.
When your site gets 10 visitors a day, it’s not worth your time to worry about how fast your pages load; but if you’re serving millions of people, every millisecond counts. So Yahoo created a team dedicated to improving user experience via performance.
Though more companies are getting savvy as online presence becomes increasingly essential, it’s still easy to find offenders. Notice how long it takes to load MSN vs. Yahoo. Yahoo is almost instantaneous – it’s incredible that a full-featured portal can be so responsive. Their efforts have really paid off.
In addition to guidelines for optimizing your own site, they distribute YSlow, a tool that plugs in to Firefox to automatically identify the ways you can improve your response times.
Browsers are limited to a fixed number of connections to download the text, graphics, and Javascript that make up a page, and you can direct browsers to use already-downloaded common files like logos and icons instead of downloading them again when displaying future pages.
According to YSlow, MSN.com could speed up their site by loading fewer files and directing browsers to cache this static content.
So here are some things you can do with lighttpd that will benefit pretty much any site.
-
Expire headers
I can’t think of any reason you wouldn’t want to cache static content. To make caching problems impossible, just include revisions in your filenames (e.g. mypic_20080324.jpg). Then uncomment mod_expire in your lighttpd.conf and add these lines.
# Expire images, static text in 24 hours $HTTP["url"] =~ "\.(png|js|css)$" { expire.url = ( "" => "access 7 days" ) }
Add or remove file types as you like. Other good ones would be jpg, gif, or truly static html.
-
Compress text files
This includes PHP, HTML, CSS, and JS files. Every browser supports gzip compression, so use it!
lighttpd 1.5 has a really cool module called mod_deflate that will take care of all this. mod_compress won’t handle dynamic content (PHP), but deflate does.
If you can’t use 1.5, you’re stuck with mod_compress, which unfortunately can’t be used in conjunction with mod_expire in 1.4. So unfortunately you have to choose between expire and compress in 1.4.
Anyway, add this to your configuration to use it.
## Deflate text content deflate.enabled = "enable" deflate.min-compress-size = 200 deflate.sync-flush = "enable" deflate.output-buffer-size = 8192 deflate.work-block-size = 512 deflate.mimetypes = ( "text/html", "text/plain", "text/css", "text/javascript", "text/xml" )
I took the homepage of my file delivery site from 50 kB per request to 3 kB using both of these. That’s a 94% reduction in data transferred! Less time to transfer files, and less system resources on both the server and client sides.
Love to hear what kind of improvements you see!