Apache Enable compression: How compression works in HTTP/Apache

“When it comes to HTTP and Apache, compression is like the superhero sidekick that helps speed up your website! You see, compression helps shrink the size of all the data that’s sent between your browser and the server. Think of it like squeezing a giant juice box into a tiny sippy cup. Same delicious juice, less space taken up. This means your website loads faster and uses less data. So, give your website a boost and turn on compression, it’s a real game-changer! “

Have you ever used a program like WinZip or WinRar to make a file smaller? Well, the compression in Apache works in a similar way.

Imagine you’re at a buffet and you want to take home leftovers, but you don’t have a lot of room in your to-go container. So, the server is like the buffet worker who packs up your meal, but instead of leftovers, it’s the data from a website you requested. The server packs up that data nice and tight, so it takes up less room in the “container” (AKA, the amount of data sent to your browser). And just like how you open up your to-go container at home, your browser “unpacks” the data and voila! Your website is displayed on your screen.

But here’s the thing, using a lot of compression can use up a lot of the server’s resources, just like how a buffet worker using too much plastic wrap can slow them down. So it’s important to find the right balance of compression and server resources. And just like how you need a certain type of container to take home leftovers, your browser needs to be able to “unpack” the compressed data.

Installing mod_deflate in Apache:

Now, if you’re using Apache 2 like me, I’ll be showing you how to install and configure the mod_deflate module. But don’t worry, even if you’re using Apache 1.3, the process isn’t too different, so you can still follow along.

First things first, during the installation of Apache, make sure to enable the deflate module. And just like how you have to pack up your leftovers before you can take them home, you’ll have to configure the mod_deflate module before you can start compressing data like a pro.”

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.

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.

Other post: Can a Bad Belt Tensioner Cause Vibration

Scroll to Top