Code: Configuring mod_deflate

Chapter 9 - Advanced Web Performance Optimization

Apache 2.0 includes the mod_deflate module instead of mod_gzip. Setting up mod_deflate is easy because it is already included in Apache 2.0. To configure mod_deflate, add the following lines to your httpd.conf file:

LoadModule deflate_module modules/mod_deflate.so
SetEnv gzip-only-text/html 1
SetOutputFilter DEFLATE

You can choose to approach configuring mod_deflate slightly differently, depending on your style. You can either explicitly include MIME types, or explicitly exclude file types from the compression routine.

This example, from the httpd.conf file, shows explicit inclusion by MIME type:

DeflateFilterNote ratio
DeflateCompressionLevel 9
DeflateMemlevel 9
DeflateWindowSize 15
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript

This example shows explicit exclusion by file extension:

SetOutputFilter DEFLATE
DeflateFilterNote ratio
DeflateCompressionLevel 9
DeflateMemlevel 9
DeflateWindowSize 15
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar|Z)$ no-gzip dont-vary

Both HTTP-compress your HTML, CSS, and JavaScript files.