Enable GZIP compression to make website load faster


GZIP compression is based on the deflate algorithm, which is a combination of Huffman and LZ77 algorithms. ‘Deflate’ was developed in response to software problems patent covering LZW and other compression algorithms, thus limiting the possible uses of compression and other popular archiving programs.

Enabling this feature could make your website pages load +70% faster.

First make sure you have this line in httpd.conf:

LoadModule deflate_module modules/

Create a new file in /etc/httpd/conf.d called deflate.conf and add the following contents.

#Set to gzip all output
SetOutputFilter DEFLATE

#exclude the following file types
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf|mp3)$ no-gzip dont-vary

#set compression level
DeflateCompressionLevel 9

#Handle browser specific compression requirements
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

ensure you include this file in your httpd.conf.

How To Install And Configure mod_deflate On CentOS

Mod_deflate is an Apache module which allows output from your web server to be compressed before being sent to the client.

Mod_deflate is included and enabled in the default Apache installation on CentOS. To confirm this run apachectl, the Apache Server Control Interface, and filter the output with grep for the keyword deflate like this:

apachectl -t -D DUMP_MODULES |grep deflate

You should see deflate_module (shared) if mod_deflate is installed and enabled.


Configure mod_deflate.conf

Plain text formats can be greatly reduced in size by compression (more than 75%), and that’s why it makes sense to apply it to HTML, CSS, or JavaScript files. However, many multimedia formats such as Flash and pictures already have compression in them, and additional compression will be futile.

create a new configuration file /etc/httpd/conf.d/mod_deflate.conf with the sample code:

<filesMatch "\.(js|html|css)$">
    SetOutputFilter DEFLATE


mod_deflate has a few of its own important configuration options:

  • DeflateCompressionLevel – the compression level to be applied. By default, this level is 9, the highest level of compression. 1 is the least level of compression. Higher compression would makes the smallest output at the price of higher server CPU usage.
  • DeflateMemLevel – the amount of memory zlib, the compressing library, can use. The default value is 9 which is also the highest value. To calculate precisely the allowed memory you should multiply the DeflateMemLevel value by 16K.
  • DeflateWindowSize – the compression window size. By default, it’s the highest possible value of 15. Higher number means higher compression level, again at the price of more server resources.

Restart apache

apachectl restart

Testing using wget

wget --header="Accept-Encoding: gzip"
ls -lah jquery-1.11.3.js
-rw-r--r-- 1 user user 83K Apr 28 12:20 jquery-1.11.3.js
ls -lah /var/www/html/jquery-1.11.3.js
-rw-r--r-- 1 apache apache 278K Apr 28 12:20 /var/www/html/jquery-1.11.3.js



Leave a Reply