Feb 222011
 

The hundreds of milliseconds — sometimes even seconds — ticking away each time you click on a web link or button are in general not caused by a lack of resources at server or client side. Only when your local PC or the remote system is busy, limitations in processing power or memory are causing extra delays. Most often, the network is the bottleneck.

That means that computer resources and network bandwidth/speed can be balanced better using data compression. For network traffic is reduced at the cost of higher workloads at the endpoints.

In this article we describe the data compression facilities available with the Apache HTTP server, and we show a basic setup.

HTTP Headers

Each time your browser software contacts a web server, it tells the other side which compression protocols it supports. For example, your browser could send a header like this:

Accept-Encoding: gzip, deflate

announcing that it will accept both gzipped and deflated content.

The server, at its turn, may then respond with a compressed reply. The compression method is denoted in a header like this:

Content-Encoding: gzip

Apache Modules

Apache, the most used web server, supports both the gzip and deflate compression methods. The former has been part of Apache since version 1.3. The latter was introduced with Apache version 2.

Although mod_gzip has been improved since, mod_deflate is currently the preferred data compression module.

Configuration

Setting up mod_deflate requires nothing more than a small configuration file in the Apache conf/ directory. On Red Hat / CentOS/ Fedora installations, the module configuration files can be found in the directory:

/etc/httpd/conf.d/

There we create a new configuration file, e.g. deflate.conf, containing the following statements:

<IfModule mod_deflate.c>

  <Location />
  # Insert filter
  SetOutputFilter DEFLATE
  # Netscape 4.x has some problems...
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  # Netscape 4.06-4.08 have some more problems
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  # MSIE masquerades as Netscape, but it is fine
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  # Don't compress images
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
  # Make sure proxies don't deliver the wrong content
  Header append Vary User-Agent env=!dont-vary
  </Location>

  # Log compression ratio  # disable after test
  #CustomLog logs/deflate_log deflate

  </IfModule>

Do not feel intimidated by this: most of the statements handle exceptions for specific browsers. You can find these in the Apache documentation for the deflate module.

Now you can restart the httpd daemon using the command:

/etc/init.d/httpd restart

or:

service httpd restart

If you want to see how Apache is performing, you can uncomment this line:

CustomLog logs/deflate_log deflate

However, after testing/playing you should disable logging for this module, as it generates quite some overhead.

Since this configuration contains no site-specific statements, these over-all settings should do the job for all your (virtual) servers. If necessary, you can also include a mod_deflate configuration with a specific virtual server.

Leave a Reply