Speed Up Your Website with Compression

In my quest for better search engine optimization (SEO) I’ve found that Google considers how fast your site loads in its ratings. There are a lot of things you can do to clean up and reduce the size of your site so that it loads faster but one of the easiest is by using good ole compression. We have been using .ZIP files and .GIF images for as long as I can remember. Why not apply it to your website.

Adding compression to your website will make it load faster. Faster makes your visitors happy. Faster makes Google happy. Compression also reduces the amount of bandwidth your site consumes making you and/or your host happy. The only downside is that it will add some load to your server as it does have to do all the compression. The load is noticeable. My server went from a typical 0.009% CPU load to a 1.5% CPU load when I turned on compression. After a day the CPU usage settled to around 0.09% CPU or roughly 10x more than it was using without compression. I’m not sure why it was so high the first day. I’ve got lots of CPU to spare but you may not be in the same boat. If your server is running at 10% CPU or more watch the CPU usage closely when turning on compression. If you overload your server you could end up serving pages slower.

After turning compression on I sampled 10 of my sites and the front pages were compressed from a low of 54.7% to a high of 89.8%. The average amount of compression was 68.3% for the 10 sites I randomly sampled. I was surprised at just how high those numbers are.

Turning on GZIP compression with an Apache2 server is super easy. Your server must have mod_deflate installed.

If you don’t have access to your servers configuration files you can turn compression on using your .htaccess file. Add this line to your .htaccess:

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

Once that is done you should test your site. Here are some popular online page compression test sites:

If you do have access to your servers httpd.conf file here is the code to turn compression on for every site on your server. This code compresses everything except the specified file types. In addition to mod_deflate you will also need mod_setenvif and mod_headers installed. They should be, if not you can just remove those sections from the code and it should still work fine.

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

<IfModule mod_setenvif.c>
# Netscape 4.x has some problems
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48, the above regex won’t work. You can use the following
# workaround (comment the above line and uncomment the below line) to get the desired effect:
# BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# file-types indicated will not be compressed
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary

The mod_deflate section does the compression work with the SetEnvIfNoCase lines specifying types of files NOT to compress. Change the code accordingly to reflect the file types to exclude. The mod_setenvif is there to provide compatibility with some browsers that don’t support compression. The mod_headers section instructs browser to append the new value (User-Agent) of the Vary header to any previous value of the Vary header (separating the new value from the old one by a comma) or to create a new value for the Vary header, if the dont-vary environment variable is undefined within the environment of the executing Apache service. It causes Apache to alert proxies that cached response should be sent only to clients that send the appropriate Accept-Encoding request header, and thus prevent compressed content from being sent to a client that does not understand it.

Remember you will need to restart your Apache server after you modify the httpd.conf file.

apachectl graceful

I suggest you start by adding the simple line of code above to your .htaccess file and testing your site. Once you are happy with the results and I’m sure you will be go ahead and modify your httpd.conf file if you have access to it.

This entry was posted in Servers, Websites and tagged , , , , , , , , , . Bookmark the permalink.

One Response to Speed Up Your Website with Compression

  1. For commandline testing you can use wget:
    wget –save-headers –header\=”Accept-Encoding: gzip,deflate” http://website.com

    Or use this good online tool:

Leave a Reply

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