Enabling compression for faster web pages

If you’re familiar with Steve Souders, or use tools like YSlow or PageSpeed to optimize your sites, you probably already know that enabling compression for your web sites is a proven way to speed up response times. Even though that’s well known by now, I remember having a hard time finding a resource that illustrated how to do this back when I first started doing this for my sites. It’s a very simple procedure, here’s what’s involved.

First, make sure you have the deflate_module installed.  To do that:

  1. find your Apache’s config file (for CentOS – /etc/httpd/conf/httpd.conf)
  2. open it and make sure you have the deflate_module installed.  Search for lines beginning with LoadModule – if you see a line starting with LoadModule deflate_module, it’s installed and ready to use.  Otherwise, you’ll need to add a line for the deflate_module.

Next, you’ll need to add AddOutputFilterByType directives to the Apache config file.   These basically inform Apache which content types you want the deflate_module to act on.  Here are the ones I’ve added:

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Keep in mind there are 2 ways of adding these directives. You can add those anywhere in your Apache config file to enable compression globally (I appended mine to the end of my conf file). However, you may only have specific apps or sites that you want to target vs. enabling compression globally; in such cases you can add those directives inside their respective virtual host blocks.

You can see the impact this change made, even to simple pages like my blog.  The entire page is now 30% smaller and 131 ms have been shaved from the total download time.

Before

After

This entry was posted in Performance. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *