How To Save Traffic With Lighttpd's mod_compress (Debian Squeeze)
|
Submitted by falko (Contact Author) (Forums) on Tue, 2011-04-05 17:12. :: Debian | Web Server | Lighttpd
How To Save Traffic With Lighttpd's mod_compress (Debian Squeeze)Version 1.0 In this tutorial I will describe how to configure mod_compress on a Lighttpd web server (on Debian Squeeze). mod_compress allows Lighttpd to compress files and deliver them to clients (e.g. browsers) that can handle compressed content which most modern browsers do. With mod_compress, you can compress HTML, CSS, Javascript, text or XML files to approx. 20 - 30% of their original sizes, thus saving you server traffic and making your modem users happier. Compressing files causes a slightly higher load on the server, but in my experience this is compensated by the fact that the clients' connection times to your server decrease a lot. For example, a modem user that needed seven seconds to download an uncompressed HTML file might now only need two seconds for the same, but compressed file. By using mod_compress you don't have to be afraid that you exclude users with older browsers that cannot handle compressed content. The browser negotiates with the server before any file is transferred, and if the browser does not have the capability to handle compressed content, the server delivers the files uncompressed. I do not issue any guarantee that this will work for you!
1 Preliminary NoteI'm assuming you have a working Lighttpd setup on your Debian Squeeze server, e.g. as shown in this tutorial: Installing Lighttpd With PHP5 And MySQL Support On Debian Squeeze
2 Enabling And Configuring mod_compressOpen /etc/lighttpd/lighttpd.conf... vi /etc/lighttpd/lighttpd.conf ... and check if mod_compress is active in the server.modules stanza (it should be active by default on Debian Squeeze):
You should also find the following two lines further down the file:
The compress.cache-dir directive tells Lighttpd to store compressed files in the directory /var/cache/lighttpd/compress/; the compress.filetype directive tells Lighttpd what file types to compress (usually Javascript, CSS, HTML, and plain text files). As Javascript files might have different file types on each server, we should extend that directive so that it looks as follows:
Next make sure that the directory /var/cache/lighttpd/compress/ exists and is owned by the user and group www-data (both should be true by default on Debian Squeeze): ls -la /var/cache/lighttpd/compress/ root@server1:~# ls -la /var/cache/lighttpd/compress/ If the directory does not exist, create it as follows: mkdir -p /var/cache/lighttpd/compress/ Now restart Lighttpd: /etc/init.d/lighttpd restart
3 Compressing PHPPHP provides compression support by itself, therefore this must not be handled by Lighttpd. To enable PHP compression, open /etc/php5/cgi/php.ini... vi /etc/php5/cgi/php.ini ... and set zlib.output_compression to On:
Restart Lighttpd afterwards: /etc/init.d/lighttpd restart
4 Cleaning Up Lighttpd's Compression DirectoryThe directory where Lighttpd stores the compressed files (/var/cache/lighttpd/compress/ by default) should be cleaned from old, unused files regularly. On Debian Squeeze, you should find a cron job that does this on a daily basis in /etc/cron.daily/lighttpd: cat /etc/cron.daily/lighttpd
This cleans the directory from files that haven't been accessed in the last 30 days. If /etc/cron.daily/lighttpd does not exist, you can create a cron job yourself: crontab -e
(This cron job would run each day at 04.23h.)
5 TestingTo test if your configuration works, you can install the Live HTTP Headers plugin for Firefox and access text file through Firefox (e.g. a static HTML page). In the Live HTTP Headers output, you should now see that the client (Firefox) sent an Accept-Encoding: gzip,deflate header to tell the server that it accepts compressed content in the formats gzip and deflate; the server should compress the file and send it with a Content-Encoding: gzip header: If you check your compression directory /var/cache/lighttpd/compress/, you should see that it's not empty anymore; it should now contain the compressed versions of the requested files: ls -la /var/cache/lighttpd/compress/ root@server1:~# ls -la /var/cache/lighttpd/compress/
6 Links
|





Recent comments
11 hours 18 min ago
17 hours 59 min ago
21 hours 50 min ago
23 hours 29 min ago
1 day 7 hours ago
1 day 17 hours ago
1 day 18 hours ago
1 day 21 hours ago
2 days 2 hours ago
2 days 2 hours ago