Code: Configuring mod_gzip

Chapter 9 - Advanced Web Performance Optimization

Here is an example addition to an httpd.conf configuration file:

# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding 'LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Please read the file http://httpd.apache.org/docs/dso.html for more
# The order in which these modules load is important...
#
LoadModule rewrite_module libexec/mod_rewrite.so
LoadModule expires_module libexec/mod_expires.so
LoadModule php4_module libexec/libphp4.so
LoadModule bwlimited_module libexec/mod_bwlimited.so
LoadModule bytes_log_module libexec/mod_log_bytes.so
LoadModule auth_passthrough_module libexec/mod_auth_passthrough.so
LoadModule gzip_module libexec/mod_gzip.so
# ...
# Reconstruction of the complete module list from all available modules
# (static and shared ones) to achieve correct module execution order.
# [WHENEVER YOU CHANGE THE LOADMODULE SECTION ABOVE UPDATE THIS, TOO]
ClearModuleList
AddModule mod_env.c
...
AddModule mod_php4.c
AddModule mod_bwlimited.c
AddModule mod_log_bytes.c
AddModule mod_auth_passthrough.c
AddModule mod_gzip.c

Note how LoadModule and AddModule mod_gzip are the last commands in the list. Next, configure mod_gzip with the minimum file size (anything less than 1,000 bytes is not worth the overhead), the maximum in-memory size directive (we chose 1,000,000 bytes as a maximum), and the types of files to include in compression:

<IfModule mod_gzip.c>
mod_gzip_on yes
mod_gzip_send_vary yes
mod_gzip_dechunk yes
mod_gzip_keep_workfiles No
mod_gzip_temp_dir /tmp
mod_gzip_minimum_file_size 1002
mod_gzip_maximum_file_size 0
mod_gzip_maximum_inmem_size 1000000
mod_gzip_item_include file "\.htm$"
mod_gzip_item_include file "\.html$"
mod_gzip_item_include mime "text/.*"
mod_gzip_item_include file "\.php$"
mod_gzip_item_include mime "jserv-servlet"
mod_gzip_item_include handler "jserv-servlet"
mod_gzip_item_include mime "application/x-httpd-php.*"
mod_gzip_item_include mime "httpd/unix-directory"
mod_gzip_item_exclude file "\.css$"
mod_gzip_item_exclude file "\.js$"
mod_gzip_item_exclude file "\.wml$"
</IfModule>

Note that we include text, .htm/.html, and .php files but exclude .css, .js, and .wml files to simplify this example. Do not HTTP-compress MP3 files (because they are already compressed), or PDF files (Acrobat Reader can have problems reading gzipped PDF files because they are already compressed internally).