How to Install and configure mod_deflate – Compress Web Content delivered by Apache
Apache prepares the response that will be sent back to the client in several stages. One of those stages involves the modification or conversion of the data using output filters. mod_deflate, once loaded and activated, inserts such a filter, namedDEFLATE, in Apache’s chain of output filters, which compresses all data that goes through it according to some rules the web server administator has defined. For instance, one can set the compression level, restrict the compression to particular MIME types or prevent some problematic web browsers or other HTTP clients from receiving compressed data from the server.
mod_deflate can be loaded like any other Apache module:
LoadModule deflate_module modules/mod_deflate.so
Load mod_deflate
mod_deflate can be loaded like any other Apache module:
LoadModule deflate_module modules/mod_deflate.so
Please note that this directive can only exist in the main server configuration.
Enable Compression
The compression of the data can be enabled for all data that goes through the DEFLATE filter orselectively depending on its MIME type.
To enable the compression for any type of content, insert the following directive:
SetOutputFilter DEFLATE
Alternatively, to define which filetypes should pass through the DEFLATE output filter use theAddOutputFilterByType directive. The following is an example:
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
Set the Compression Level
Generally, the deflate compression algorithm is fast enough, so setting the compression level to the maximum (9) will not cause any noticeable trouble, even to relatively old hardware.
DeflateCompressionLevel 9
Custom Rules for problematic browsers
The compression can be turned-off or be restricted to files of type text/html for known problematic web browsers. These are taken from the official documentation.
BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Keep track of the compression
Finally you can keep track of the compression in order to evaluate the effectiveness of the use of mod_deflate in your server.
The following directives define some variables, such as:
- instream : the size in bytes of the data as received by the DEFLATE filter.
- outstream : the size in bytes of the compressed data as returned from the DEFLATE filter.
- ratio : the compression ratio,
(Output/Input)x100
DeflateFilterNote Input instream DeflateFilterNote Output outstream DeflateFilterNote Ratio ratio
Finally, you can define a custom logformat so to be able to record the aforementioned values to a logfile:
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
The deflate logformat can be used for the main server’s or for any vhost;s logfile.
Effectiveness of Compression
It is well known that not all document types can benefit the same from compression. Generally, the deflate algorithm can compress text surprisingly fast and with a very high efficiency ratio. On the other hand, it is almost useless when used to compress images which have been prepared for the web such as PNG, JPEG, GIF and generally all other image types in which the data has already been compressed. The same goes for compressed audio files, such as MP3, AAC, OGG, videos, PDF documents and all other already compressed files.
So, the benefits of using mod_deflate to reduce the bandwidth usage and speed up the content delivery are heavily dependent on the type of files your web server delivers.
Browser Support
A web server that sends compressed data to clients would be completely useless if the HTTP clients couldn’t decompress that data. All modern and popular web browsers support accepting content that has been compressed using the gzip or deflate algorithms, so there should be no problem at all.
Appendix I
Here is the complete mod_deflate configuration as described in this article. Save it in a file, named deflate.conf and import it in the main server’s configuration using the Include directive
(Include /path/to/deflate.conf):
<
#
# mod_deflate configuration
#
LoadModule deflate_module modules/mod_deflate.so
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
DeflateCompressionLevel 9
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
</IfModule>
This configuration will be inherited by all virtual hosts.
To disable it just comment out the line that loads the mod_deflate module (#LoadModule ...).
To record mod_deflate’s specific variable (instream, outstream, ratio) values for a virtual host, just add a new log file of type deflate:
CustomLog /path/to/vhost/logs/deflate_log deflate
This will give you an idea of how efficient is the use of mod_deflate in that particular vhost.
Tags:administator, apache, application javascript, compression algorithm, compression level, conversion, filetypes, mime type, output filter, problematic web, server configuration, web browsers, web server

