Mod Deflate - Compress Your Web Pages and Save Bandwidth | WebReference

Mod Deflate - Compress Your Web Pages and Save Bandwidth

By Sukrit Dhandhania


When you get a good number of visitors on your website, you can end up with a rather large Internet bandwidth bill from your web hosting company. This is usually a good problem to have as it means your website is generating traffic. However, there are some steps you can take to try to optimize your website so that it consumes less bandwidth per user. There are a number of ways to do this. Let's look at one such solution for Apache. We'll learn how to setup and use the Apache module mod_deflate. This module adds an output filter that allows output from your server to be compressed. A great side effect of the implementation of this module is that it also speeds up your website.

This module is called mod_deflate for Apache 2.x installations, and was called mod_gzip for Apache 1.3. In this article, we'll look at setting up, configuring, and testing mod_deflate on Apache 2. If you have Apache 2 installed on your system, you should have mod_deflate already installed also. You should check if the module has been loaded in Apache. Open the Apache server configuration file, httpd.conf, and search for the word mod_deflate in the LoadModule section of the configuration. The line should looks something like the following:



LoadModule deflate_module modules/mod_deflate.so 

If you don't find something similar append the line at end of the LoadModule section of the configuration file. Restart Apache to activate the change with the following command:

# service httpd restart 

Assuming Apache restarted correctly, we can proceed with the configuration of the mod_deflate module. At this point, you should note that there are some older web browsers such as Netscape 4 that do not support mod_deflate. When we configure mod_deflate we need to instruct it to not use compression for these web browsers. You can enable mod_deflate either for your entire Apache setup, or only for individual virtual hosts. Depending upon this, add the following configuration changes to the main Apache configuration file, httpd.conf, or to your virtual host configuration files.

The first thing to do is to setup mod_deflate to compress HTML, text, and XML files. Add the following line to your configuration file:

AddOutputFilterByType DEFLATE text/html text/plain text/xml

mod_deflate supports a lot more customized configuration. If you can, specify how you want it to handle different file formats. For example, compressing image files is quite pointless, as the compression will give you little or no difference in file size. Therefore, you can use the following lines in your mod_deflate configuration to tell it not to compress image, PDF files, and files that have already been compressed using zip or rar...

As I mentioned earlier, certain web browsers cannot handle compression. Add the following line to your configuration file to tell it not to use compression if the user is using those browsers:

There are a few other standard configuration parameters that you should set. These will set the compression ratio and logging settings of mod_deflate. Add the following to your configuration. Note that you should change the path of the mod_deflate log from /var/log/httpd/deflate_log to the location of the Apache logs on your machine.

Once you are done making the changes you need to restart Apache. A safe way to do that is using the command apachectl. This command allows you to first run a test to see if your configuration changes are breaking anything. Then you can do the restart. Use the following steps:

If your web server restarts properly, the compression should now be in place. Let's check the logs to see if logging is working fine. Open a web browser and go to the website where you have just setup compression. Next, look at the mod_deflate logs to see if there are any entries. Open deflate_log in a text editor and see if you get an entry resembling the following:

"GET /reading.php HTTP/1.1" 7621/45430 (150%)

If you see something like the line above in your log file, compression is working just fine. As you can see in the line above, the file reading.php is being compressed 150%. Now we move on to the section where we run a couple of tests to see just how effective compression is on your website.

Now that we have mod_deflate all set up it's time to run some test to see how much the compression helps. There are several ways to do this. We'll try out a couple of methods. The first is using What's My IP mod_gzip Tester a web based service, and the other is using the YSlow plugin for Mozilla Firefox. Go to the What's My IP mod_gzip Tester website and enter the URL of your website. It should return with a note saying that your compression is enabled and some stats on how much compression is happening:

http://www.example.com is gzipped Original Size: 7.55 KB 
Gzipped Size: 2.34 KB Data Savings: 69.01% 

For the second test, you will need Mozilla Firefox. You will also need to download the the YSlow plugin for Mozilla Firefox. Restart Firefox to enable the plugin. Once you do that, go to your website using Firefox and click on the YSlow icon at the bottom right of the application's window. Note down the total size of the downloaded files as well as the time taken to download them. Run the tests a few times. Now roll back the mod_deflate settings and see how much of a difference it makes. There are other measures you can take to help reduce the bandwidth usage on your website, but this is a good start. Now your website should consume less bandwidth and the users will also get a better surfing experience as the pages will load a little faster.

Original: September 16, 2009