It is important to load quickly every website page. In this article, I am going to describe the configuration of mod_deflate for Apache 2. This module let’s you to compress the content that the server is sending to clients (web browsers, in general). By doing it, the pages will load more quickly.

First, look for this module in your current Apache configuration file, for example:

grep deflate_module httpd.conf

if Linux returns:

LoadModule deflate_module modules/mod_deflate.so

then, this module is added in Apache.

Then, you must decide which content need to be compressed by this module. To compress text, html and xml files, add this code to your website definition code:

AddOutputFilterByType DEFLATE text/html text/plain text/xml

but, if you want to compress all files types by excluding only certain of them, try it:

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary

This would compress all files except images (gif, jpg, and png), already compressed files (like zip and tar.gz) and PDF files which makes sense because you do not gain much by compressing these file types.

For any of these filter definitions, you must add this code:

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

that is going to be used for old servers that not support compression for files no others than html documents.

It finishes the configuration, so you can restart Apache.

To test this module, add the following code to the configuration of your mod_deflate module.

DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat ‘”%r” %{output_info}n/%{input_info}n (%{ratio_info}n%%)’ deflate
CustomLog /var/log/apache2/deflate_log deflate

Make sure to match correctly the path of “deflate_log”.

Test your configuration file: apachectl configtest and restart apache if all is fine.

This output:

“GET /info.php HTTP/1.1″ 7621/45430 (16%)

will guide you to know if this compression method is working.

It is all, have fun!