
Continuing on with our optimization mini-series, today we’ll be enabled GZIP on out server. GZIP compresses files before sending them to the browser (which will decompress them automatically), which results in a smaller download size and, therefore, a faster loading file.
However, GZIP does have some downfalls. First of all, compresses files as they’re requested (everytime, on-the-fly) can become very CPU-intensive on a very large site. Secondly, on very small files, the time the browser takes to decompress the file might be longer than the time saved downloading. These won’t become an issue, as your site would have to be very large, and your server very underpowered for the first situation to become pronounced. Since the download time on small files is very minimal anyways, you can just throw the second situation off the table.
As we have compiled Apache with the mod_deflate module, enabling GZIP is fairly easy. Start off by opening up /usr/local/apache2/conf/httpd.conf and finding the <Directory /home/*/public_html> line. Before the </Directory> line, add the following lines:
# Send gzipped content
SetOutputFilter DEFLATE
# Only compress text/html for non-supporting browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
These lines will enable GZIP compression for all files. Because some old browsers did not support GZIP compression on all file types, the last three lines will selectively disable it. If the browser doesn’t specify that it will accept GZIP compressed content, Apache will automatically disable GZIP for that specific request.
This will currently enable GZIP for all file types, including images. Unfortunately, compressing images with GZIP will actually increase their file size, giving you the opposite effect. The solution is to disable GZIP compression on images. Add these lines outside of the directory tags:
SetOutputFilter OFF
Save and close the file. Then restart Apache.
There you have it, faster downloading web pages using GZIP compression.




