mod_deflate

mod_deflate overview

mod_deflate module applies DEFLATE filter that allows server output to be compressed before sending it to the client over the network.

Examples of mod_deflate implementation

Compress only specific types

AddOutputFilterByType DEFLATE text/html text/plain text/xml 

Compress everything except images

<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>

Enabling Compression

Output Compression

Compression is enabled by DEFLATE filter as follows:

SetOutputFilter DEFLATE 

Some browsers do not support compression of all content types, so you may need to set gzip-only-text/html to 1 to only allow compression of html files. If you put other value than 1, it will be ignored.

If you want to restrict the compression to particular MIME types, you may use AddOutputFilterByType directive:

<Directory "/your-server-root/manual">
      AddOutputFilterByType DEFLATE   text/html
</Directory> 

For those browsers that have problems with compression of all file types it may be helpful to use BrowserMatch directive to force no-gzip for that particular browsers. You may combine no-gzip with gzip-only-text/html to get the best results. In that case the former overrides the latter:

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

At first we probe for a User-Agent string that indicates a Netscape Navigator version of 4.x. These versions cannot handle compression of types other than text/html. The versions 4.06, 4.07 and 4.08 also have problems with decompressing html files. Thus, we completely turn off the deflate filter for them.

The third BrowserMatch directive fixes the guessed identity of the user agent, because the Microsoft Internet Explorer identifies itself also as "Mozilla/4" but is actually able to handle requested compression. Therefore we match against the additional string "MSIE" (\b means "word boundary") in the User-Agent Header and turn off the restrictions defined before.

Note! The DEFLATE filter is always inserted after RESOURCE filters like PHP or SSI. It never touches internal subrequests.

Note! There is a environment variable force-gzip, set via SetEnv, which will ignore the accept-encoding setting of your browser and will send compressed output.

Output Decompression

mod_deflate module also provides a filter for inflating/uncompressing a gzip compressed response body. In order to activate this feature you have to insert the INFLATE filter into the output filter chain using SetOutputFilter or AddOutputFilter:

<Location /dav-area>
      ProxyPass   http://example.com/
      SetOutputFilter INFLATE
</Location>

This code will uncompress gzip'ed output from example.com, so other filters can do further processing with it.

Input Decompression

mod_deflate module also provides a filter for decompressing a gzip compressed request body. In order to activate this feature you have to insert the DEFLATE filter into the input filter chain using SetOutputFilter or AddOutputFilter:

<Location /dav-area>
      SetInputFilter   DEFLATE
</Location> 

Now if a request contains a Content-Encoding: gzip header, the body will be automatically decompressed. Few browsers have the ability to gzip request bodies. However, some special applications do support request compression, for instance some WebDAV clients.

Dealing with proxy servers

mod_deflate module sends a Vary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to clients that send the appropriate Accept-Encoding request header.

If you use some special exclusions dependent on, for example, the User-Agent header, you must manually configure an addition to the Vary header to alert proxies of the additional restrictions. For example, in a typical configuration where the addition of the DEFLATE filter depends on the User-Agent, you should add:

Header append Vary User-Agent 

If your decision about compression depends on other information than request headers (e.g. HTTP version), you have to set the Vary header to the value *. This prevents compliant proxies from caching entirely:

Header set Vary *

mod_deflate directives

DeflateCompressionLevel

Description: Defines to what extent the content must be compressed
Syntax: DeflateCompressionLevel value
Default: 6
Context: server config, virtual host, directory, .htaccess
Module: mod_deflate

DeflateCompressionLevel directive sets the level of compression to be applied to the content. Higher value means better compression.

Compression level value must be between 1 (minimum compression) and 9 (maximum compression).

Example:

DeflateCompressionLevel 3