How to Speed Up Your Website with GZIP Compression

Every time a user accesses your website, the user’s browser must go through the motions of downloading your content. In many cases, this happens very quickly, but it still takes time. Although the browser may cache some files, it still has to download any new content, and modern websites publish new content very often.

GZIP compression is a way to deliver content to your visitors in a compressed format. This saves bandwidth for you and also saves the users time by making the website seem faster. Files are compressed in GZIP format, an open compression format, and then the the users’ browsers extract the files.

Setting GZIP compression in Apache only takes a few configuration changes. The good news is that many Linux distributions, such as CentOS have mod_deflate installed by default for Apache. In the case of CentOS, however, it is not active. All you have to do is create a file with the necessary settings at /etc/httpd/conf.d/deflate.conf:

# Insert filter
SetOutputFilter DEFLATE

# 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

# Don’t compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

# Don’t compress already compressed stuff !
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary

# Log Stuff !
# DeflateFilterNote Input input_info
# DeflateFilterNote Output output_info
# DeflateFilterNote Ratio ratio_info
# LogFormat ‘”%r” %{output_info}n/%{input_info}n (%{ratio_info}n%%)’ deflate
# CustomLog /var/log/httpd/deflate_log deflate

Save the file, restart Apache, and you should now have GZIP compression up and running.