Enable Gzip Compression In Apache HTTPD With Mod_Deflate

Author: Steven Neiland

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

GZip compression is a simple, effective way to save bandwidth and speed up your site. For anybody using Apache HTTPD here is a simple walk through of the steps required to enable it.

Note that there is a side effect of enabling gzip compression you need to be aware of.

Beware Higher CPU Overhead

By adding a compression step to your server you are increasing the cpu cost of each resource request. If your server is already under heavy load this additional overhead could be a serious downside. Only you can judge if the benefits outweigh the costs.

So lets get started.

Step 1: Enable mod_deflate

The first step to adding compression is to install the mod_deflate module. If you dont have it already then you can download it from the module page of the apache httpd project website and add it to your httpd modules directory. Slackware comes with this module already present.

Once you have confirmed the mod_deflate module exists open the "httpd.conf" file and either add or un-comment the LoadModule directive for mod_deflate.

Slackware Conf File Location

#uncomment this to enable mod_deflate
LoadModule deflate_module modules/mod_deflate.so

Step 2: Create a Deflate Rule Set

With the module enabled we must now tell Apache HTTPD what type of files we want it to compress. To do this we create a file named httpd-deflate.conf (this is my naming convention but you can actually name it anything you want).

vi /etc/httpd/extra/httpd-deflate.conf

Lets tell Apache that we want to gzip all files except those file formats which we know are already compressed. We also put in some browser specific compression rules. Copy the following code into the httpd-deflate.conf and save it.

#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

Step 3: Load Deflate Rule Set

At this stage you can choose to load our deflate rule set for all sites or per vhost. To load the rule set it for all sites place this line in httpd.conf.

# Deflate configuration
Include /etc/httpd/extra/httpd-deflate.conf

If you just want to use these rules for one website put this command in the body of that individual vhost in the "httpd-vhost.conf" file.

Step 4: Restart Apache HTTPD

Finally we save our changes and restart apache httpd. For Slackware folks here is the restart command for apache.

/etc/rc.d/rc.httpd restart

Alternative Config: Deflate Named Types

In my previous deflate file we compressed everything except a list of excluded types. An alternative approach is to only compress for named types (for example html, css, js). Here is a deflate rule set I have used this approach.

#set compression level
DeflateCompressionLevel 9

#compress these named file types
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-js
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/javascript

#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

Related Blog Postings

Reader Comments

mark's Gravatar
Friday, December 13, 2013 at 6:54:40 AM Coordinated Universal Time

I am curious why you dont deflate image files.


Xscratch's Gravatar
Sunday, January 5, 2014 at 5:00:29 AM Coordinated Universal Time

jpg and png are already a compressed format, so gzipping them does not produce a improvement but instead you have the overhead of a new compression (on every file request).
You can try it for yourself compressing a png or a jpeg with WinZip (or WinRar, or 7zip...).

felipe1982's Gravatar
Thursday, January 22, 2015 at 8:25:56 AM Coordinated Universal Time

I think ISO files can be compressed, as can exe files in some cases. Tar files are not compressed on their own, they need a compression algorithm such as gzip, bzip2, or others.

Kostas's Gravatar
Tuesday, October 6, 2015 at 3:47:08 PM Coordinated Universal Time

Very useful, thank you.
For jpg and png you can try jpegoptim and pngcrush.

prathap's Gravatar
Monday, December 19, 2016 at 12:44:32 AM Coordinated Universal Time

its not working on HTTPS protocol

SmartUser's Gravatar
Wednesday, February 15, 2017 at 8:29:52 AM Coordinated Universal Time

we are deploying our UI code on 80 port of tomcat and java code on 8084 port but if we enable compression for 80 port then we lost compression for 8084 port.... could someone help resolving this as we want compression on both ports

Sowmya's Gravatar
Monday, November 19, 2018 at 11:39:17 PM Coordinated Universal Time

Iam trying to run apache http server in my linux device when I am giving httpd start command to run httpd I am getting error " Invalid command 'DeflateCompressionLevel', perhaps mis-spelled or defined by a module not included in the server configuration " Please could any one help me in solving and I am not able to run my server.

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing