lighttpd In-Memory gzip Compression
Posted 2009-04-20 in WWW by Johann.
gzip compression improves website performance by decreasing the size of files. The efficiency of gzip can be increased by using an in-memory file system.
Web Application Performance Tuning Targets
There are three goals when tuning the performance of websites:
- Making less requests. Requests can be saved by inlining CSS and CSS sprites.
- Avoiding duplicate requests. This is what caching headers are used for.
- Reducing the amount of data. Either by removing whitespace or unnecessary code or by compressing the data.
Configuring lighttpd
lighttp supports transparent gzip compression out-of-the-box with the mod_compress module. Enable it in /etc/lighttpd/lighttpd.conf
like this:
server.modules = ( "mod_accesslog", "mod_access", "mod_redirect", "mod_rewrite", "mod_evhost", "mod_proxy", "mod_compress", "mod_expire" )
The next step is to set up a directory in an in-memory file system that caches the compressed version of the files.
# mkdir -p /dev/shm/lighttpd/compress # chown -R www-data:www-data /dev/shm/lighttpd
Configuring mod_compress consists of specifying the cache directory and the MIME types of the files to compress. In my example, I’m compressing plain text files, static web pages, CSS style sheets and JavaScript files.
compress.cache-dir = "/dev/shm/lighttpd/compress" compress.filetype = ( "text/plain", "text/html", "text/css", "text/javascript" )
After restarting lighttpd, gzip compression is active. Of course, only user-agents that ask for gzip-compressed content using an Accept-Encoding
HTTP header will be served the gzip compressed files.
To make sure the in-memory directory exists after reboot, add the commands above to /etc/rc.local
.
Real-Life Examples
Here are two real-life examples how much data can be saved using gzip compression:
nebel.org
NEBEL consists of very few elements.
/index.html
was compressed to 2.6 KB from 15.6 KB.https://johannburkard.de/resources/css/r5.css
was compressed to 2.4 KB from 8.3 KB.
The total bandwidth saving is 18.9 KB.
2 comments
#1 2009-04-29 by wei tsang
very cool, but remember that stuff in the shm area gets blown away on a system reboot (since it's a ramdisk :). to fix this, create a file in /etc/cron.d called 'lighty-gzip-dir' or whatever you like:
"""
# create lighty compressed content dir on system startup
MAILTO=<your email address>
@reboot root mkdir -p /dev/shm/lighttpd/compress; chown -R www-data: /dev/shm/lighttpd
"""
And remember, you need to change www-data to 'httpd' or whatever user your lighttpd process runs as.
shouldn't be a problem if your server never goes down, but it's the sort of trivial thing that bites you in the ass if you're not careful.
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/