Sunday, March 30, 2014

Configure mod deflate for Apache 2.2.x

Configure mod deflate for Apache 2.2.x

Mod Deflate comes built into Apache, but is not enabled by default. This tutorial will explain the simplest way of enabling it and setting which mime times to compress. Mod Deflate will increase your server load, but decreases the amount of time that clients are connected and can usually reduce the page size by 60 to 80 percent.

Loading Mod Deflate

First make sure that you are loading, this line should be at the top of your httpd.conf file and is usually loaded by default.

LoadModule deflate_module libexec/apache22/

Mod Deflate Settings

Second create a new config file to keep the deflate options in.

# vi /usr/local/etc/apache22/Includes/mod_deflate.conf

This file will be included in the main httpd.conf file. Inside the file add the following:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/js application/javascript application/x-httpd-php

#Highest 9 - Lowest 1
#DeflateCompressionLevel 9

#Optional - Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

#Optional - Logging
#DeflateFilterNote ratio
#LogFormat '"%r" %b (%{ratio}n) "%{User-agent}i"' deflate
CustomLog /var/log/apache/mod_deflate.log deflate

The compression level can be adjusted. 9 gives the highest compression, the smallest file sizes, and also uses the most CPU cycles.

Once you have this config file added restart apache.

# apachectl graceful

Testing Mod Deflate

To test your compression you can compare the file size by looking at the apache logs or a tool use as port80 tools.
Compressing additional types

You can compression additional mime types coming out of your server by adding them to the AddOutputFilterByType list in the mod_deflate.conf file. Some you might want to add are text/css, application/x-javascript, text/xml, and any others that are would benefit from being compressed.

You can also use telnet command to test if the gzip compression (mod_deflate) has been used or not.

No comments: