5. Compressed delivery

There are basically two modules available for output compression: mod_gzip and mod_gunzip. They are using different approaches to reach the the goal of bandwidth reduction.

mod_gunzip expects compressed file on the filesystem, and uncompress them if the browser cannot handle compressed data. The benefit is a low cpu-usage, because most browsers are capable to handle gzipped content. On the oder side, most of today's content is served dynamically i.e. PHP, and this content will be delivered uncompressed.

mod_gzip does not need compressed files on the system, all defined content will be compressed before delivery. The benefit is to have the dynamically generated content also compressed, the other side is a higher cpu-usage, because every request has to be compressed on-the-fly. Mod_gzip can handle already compressed data i.e. index.html.gz and send it as-is.

The conclusion: You carefully have to make a decision which of the two modules makes more sense for you. If you have to pay for every GB delivered and CPU-power does not matter, then mod_gzip is the choice for you. If response time matters (delay between request and delivery), and your bandwidth is cheap or unlimited, mod_gunzip matches your needs better.

A good page that helps you to make this decision is Martin Kiff's document about mod_gunzip http://www.innerjoin.org/apache-compression/howto.html

5.1. mod_gzip

5.1.2. Building and installing

To successfully compile mod_gzip you need to edit the Makefile and provide the correct path to apxs

make install

5.1.3. Sample configuration

Put the following in your /usr/local/apache/conf/httpd.conf:

Example 5. /usr/local/apache/conf/httpd.conf

mod_gzip_on                 Yes
mod_gzip_can_negotiate      Yes
mod_gzip_dechunk            Yes
mod_gzip_minimum_file_size  600
mod_gzip_maximum_file_size  0
mod_gzip_maximum_inmem_size 100000
mod_gzip_keep_workfiles     No
mod_gzip_temp_dir           /usr/local/apache/gzip
mod_gzip_item_include       file \.html$
mod_gzip_item_include       file \.txt$
mod_gzip_item_include       file \.jsp$
mod_gzip_item_include       file \.php$
mod_gzip_item_include       file \.pl$
mod_gzip_item_include       mime ^text/.*
mod_gzip_item_include       mime ^application/x-httpd-php
mod_gzip_item_include       mime ^httpd/unix-directory$
mod_gzip_item_include       handler ^perl-script$
mod_gzip_item_include       handler ^server-status$
mod_gzip_item_include       handler ^server-info$
mod_gzip_item_exclude       file \.css$
mod_gzip_item_exclude       file \.js$
mod_gzip_item_exclude       mime ^image/.*

You may whish to log the result of the compression to your accesslog. This can be done by changing the LogFormat directive in /usr/local/apache/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" mod_gzip: %{mod_gzip_compression_ratio}npct." combined

5.2. mod_gunzip

5.2.1. Download the source

Origin-Site: http://www.oldach.net/mod_gunzip.tar.gz

5.2.2. Building and installing

tar -xvzf mod_gunzip.tar.gz
cd mod_gunzip-2

/usr/local/apache/bin/apxs -i -a -c -lz mod_gunzip.c

5.2.3. Sample configuration

Put the following in your /usr/local/apache/conf/httpd.conf:

Example 6. /usr/local/apache/conf/httpd.conf

AddType text/html .htmz
AddHandler send-gunzipped .htmz

Now you can gzip your html files and rename them to i.e:

gzip index.html
mv index.html.gz index.htmz

Of course you have to change all links to htmz, i.e. <a href="page.htmz">Some page</a>