As my site is getting heavy day by day so few days back i thought of putting on http compression on my site. I did a little research and find out that i could use two modules to achieve my goal:

  • mod_gzip
  • mod_deflate

But what i didn’t find is the difference between these two modules and which one i could use with my apache(apache 2). So i thought of sharing my experience and knowledge with you for the same.

If you are using the apache 1.3 (which you should upgrade right now because it’s way too outdated) then you should be using mod_gzip (although you can use mod_deflate also) and if you are using apache 2+ then mod_deflate is the module which you should be looking for.

The performance of mod_deflate is much better than mod_gzip because of the fact that mod_deflate does not use temporary files, but instead does everything in memory.

One more thing i want to make clear that please disable the PHP compression from php.ini by setting this “zlib.output_compression = Off”. If you left it enable then for PHP files it will use this compression and which is not that much efficient what much mod_deflate is. (Tested and verified)

How compression works in HTTP/Apache:

Everyone must have heard of winzip or winrar program and how they zip the content of any file. This is the same way the compression in apache works.

When you request some data through your browser from some website then the server compress the content you had requested in the fly and then sends the data to your browser which will be de-compressed on the fly by your browser and you gets the content displayed in your browser.

From the above procedure there are few things which are clear:

  • When the server compress the data then it will be using the server resources for that, that means the more level of compression you are using on your server, the more it will be using the resources. So this is your task to find out the balance between the compression level and server resources so that your server don’t die out of compression only.
  • The second thing which is clear from the procedure is that your browser should be compatible for the de-compressing the data which all browsers support in current.

Installing mod_deflate in Apache:

As i am using apache 2 and advise the same to you also, so i will be explaining the configuration procedure for mod_deflate instead of mod_gzip. Although the procedure of both shouldn’t differ much so you can follow this procedure for getting the mod_gzip module to work with apache 1.3. Following are the steps which i followed to get the mod_deflate module working:

Installation

  • First procedure is to compile your with the source at the installation of apache. This can be done with enabling the deflate module at the command line like this.

# ./configure –prefix=/usr/local/apache –enable-so –enable-rewrite –enable-ssl –enable-expires –with-ssl=/usr/lib/openssl –enable-deflate

# make

# make install

This will compile the deflate module statically in the HTTP/Apache code. For more details about configuration and installation of apache, plz read this article installing-apache-mysql-php-linux.

  • The second procedure is to load the mod-deflate module dynamically at runtime. Get/Download the .so file for the module, place it into your apache modules directory and then add this line into your httpd.conf.
LoadModule deflate_module modules/mod_deflate.so

This will load the mod_deflate module every time at the startup of apache.

Configuration:

Configuring apache to use the mod_deflate module and compress files is the next part. To configure you need to add few lines to your httpd.conf file.

<Location />
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png|swf|pdf|exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
</Location>

This is the configuration which i am using. Few details about this configuration are:

  • In this approach i am compressing all the files and specifically excluding those which i don’t want to compress.
  • The first line in the block will tell that enable the deflate module for this location and following are the parameters which must be used.
  • The second, third and fourth line will tell apache not to compress content for the browsers which don’t support or have problems with the compressed data.
  • The fifth line will tell apache not to use compression module in the extension given.
  • You can add one more line in this “DeflateCompressionLevel value” (here the value must between 1 (less compression) and 9 (more compression).) but i will suggest that you should leave this whatever is default unless you know what you are doing. I had explained for the same in the starting.
  • You can add one more line in this “DeflateMemLevel value” (here the value must between 1 and 9). This can restrict how much memory should be used by zlib for compression. For this also i will advice that you leave the default settings.

There are various other approaches which you can also use if you don’t want to use the above one:

1. If you want to just compress some specific files only

AddOutputFilterByType DEFLATE text/html text/plain text/xml

This will compress only html/plain/xml files.

2. If you want to compress file form some specific directory only then you can use this

<Directory “/specific-directory/”>
AddOutputFilterByType DEFLATE text/html
</Directory>

This will compress only html files from the specific directory.

That’s it. Restart the apache and enjoy the speed of your compressed website.

Verify your compression:

Once you had configured your server, test whether your server is really compressing the content and serving your purpose.

  • YSlow: This is a plugin for firefox. This is what i use for all tested related my website performance. Install this plugin and verify with this.
  • Online: Use this online testing website whether it’s working or not.
  • Headers: See the headers of your content. You can use a lot of different ways to do that. I use this plugin in firefox to test my stuffs.

***** The performance of the apache will be much better if we use static compilation of module instead the dynamic loading.

See Also: mod_deflate home page

If you enjoyed this post, make sure you subscribe to my RSS feed!!!!

No related content found.