Speed Webpages Using Mod_Deflate Module On Apache

Deflate is a lossless data compression algorithm. The mod_deflate module for apache, which is based on this algorithm, compresses the contents before delivering them to the clients and thus speeds up the webpages. This module can compress up to 70% of a file. In current versions of Apache this module is installed and enabled by default. This article shows how this module can be configured in Red Hat Enterprise Linux to be used with Apache to compress files before delivering them to the clients.

Enable mod_deflate

Your current version of Apache may already have enabled this module for you, but you may still want to be sure. To enable this module, add the following line (if it does not exist) in Apache configuration file, i.e. /etc/httpd/conf/httpd.conf:

 

LoadModule deflate_module modules/mod_deflate.so

Compressing files using DEFLATE filter

The mod_deflate module provides the DEFLATE filter. The compression is implemented using this filter. To compress a few types of files, for example html and xml files, add the following line in your Apache configuration file, i.e. /etc/httpd/conf/httpd.conf.

AddOutputFilterByType DEFLATE text/html text/plain text/xml

The AddOutputFilterByType Directive assigns an output filter to a particular MIME type. In this configuration, the DEFLATE filter is assigned to MIME type text/html, text/plain and text/xml (MIME type is a way of describing the type of document. It has a major type and minor type separated by a forward slash. In our example, text is major type and html, xml etc. are minor type).
You might want to compress other file types as well by associating the DEFLATE filter with them. Add similar lines to the configuration corresponding to other file types:

#javascript:
AddOutputFilterByType DEFLATE application/x-javascript
#CSS:
AddOutputFilterByType DEFLATE text/css
#XHTML:
AddOutputFilterByType DEFLATE application/xhtml+xml

But if you have many types of file, you might want to compress almost all of them except a few. So first we set the output filter as ‘DEFLATE’ using:

SetOutputFilter DEFLATE

The SetOutputFilter directive in the above line sets the DEFLATE filter. Now DEFLATE processes the output before sending them to clients. It means that all the output is filtered (compressed) by default.
Now we need to exclude a few files that we don’t want to compress. Let’s say we don’t want to compress images. These file types can be excluded using SetEnvIf directive. But SetEnvIf matches case. For ignoring case SetEnvIfNoCase directive can be used. Now, to exclude image files from being compresses, add the following line to the configuration:

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

This will match the files whose names end with gif, jpg, jpeg or png, and prevent their compression. Other file types (such as audio, video or exe files) can be excluded similarly.
Some browsers have problems with compression of all file types. BrowserMatch directive can be used for such browsers:

BrowserMatch ^Mozilla/4 gzip-only-text/html

This will compress only text files for Netscape 4.x browser. You can also specify ‘no-gzip’ instead of ‘gzip-only-text/html’ or both as well.