Posted by Steve on Wed 11 May 2005 at 18:29
If you run a website which is mostly serving static files you can save a lot of bandwidth, in exchange for a slightly higher CPU load, by compressing the files you send to visitors. Both Apache and Apache2 allow this to be setup easily, although the method differs. Here we'll explain how this is achived with Apache2.
Traditionally mod_gzip has been used to compress files which are sent to client browsers on Debian platforms, and this process is well understood. But with the advent of Apache2 this module is no longer available, and a new technique is required.
For the Debian Apache2 package the alternative is to use mod_deflate.
The principle of both modules is the same, if a visiting web browser requests a page and tells the server that it supports compression then each of the documents which are sent back to that client will be compressed.
Naturally compressing documents as they are being served is going to result in a higher CPU usage than merely sending "plain" documents. But when the bandwidth savings are significant this tradeoff can be worth it.
To avoid compressing files which wouldn't benefit from this both modules allow you to exclude particular filetypes. This avoids trying to compress image files, which are typically compressed already.
If you've installed one of the Apache2 packages then you'll already have mod_deflate available, although it is not enabled by default.
To use it you must enable it, and then setup the configuration directives.
To enable an arbitary module with Apache2 you run:
a2enmod "modulename"
(a2enmod stands for "Apache2 enable module").
So to enable the deflate module you should run, as root:
root@lappy:~# a2enmod deflate Module deflate installed; run /etc/init.d/apache2 force-reload to enable.
To complete this job you should reload the server as the output message informs you. However we still need to setup the configuration options, so we'll not restart it just yet.
To configure the module in a basic fashion you merely need to give it a list of the type of files you wish to serve compressed (if the client browser supports it.)
You can do this by adding the following example line to your global server configuration section, or to your virtual host in /etc/apache2/sites-enabled/*:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
This informs the module that three MIME-types will be compressed, HTML files, Text files, and XML files.
If you add this to the "global" section then it will apply to all virtual hosts upon the server. Applying it to only one virtual host at a time is a useful thing to do, as it allows you to quickly see if the CPU overhead for the compression outweighs the benefits.
To allow you to keep track of the compression also add the following:
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog /var/log/apache2/deflate_log deflate
This will log the deflation ratios to the file /var/log/apache2/deflate_log. A sample log looks something like this:
"GET / HTTP/1.0" 4407/19057 (23%)
Here we served only 4407 bytes to the client, instead of 19057 bytes. That's a compression ratio of 23%. Not bad for ten minutes work!
There is one caveat to note. Some older browsers don't support compressed documents unless they are HTML files. To avoid this you should insert the following configuration directives:
BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
This should identify older version of Netscape Navigator and disable it for them. (The final line recognises Internet Explorer which also sends 'Mozilla/4' as part of it's user-agent string - but can handle the compression properly.
If you wish to tweak your setup further there is comprehensive documention on Apache's mod_deflate page.
This article can be found online at the Debian Administration website at the following bookmarkable URL:
This article is copyright 2005 Steve - please ask for permission to republish or translate.