[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Global deflate.conf for Apache

Published on
10,822 Points
Last Modified:
Community Pick
Michael Worsham
Well-rounded and highly experienced with a professional background in cloud/infrastructure solutions and project management.
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information.

The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network. Using the mod_deflate module, you can compress HTML, cross-side scripting (CSS), JavaScript, XML and other text-related files as high as 40% of their original size, thus reducing overall server network traffic. Although, the compression of the data may result in a higher CPU load of your LAMP/Web server, this overhead is to be expected as the server is having to do the compression work rather than forcing the user's end client (browser).

I designed a deflation configuration that can be dropped in the /etc/httpd/conf.d directory (commonly found on RHEL, Fedora and CentOS distros), so that is has global mod_deflate capabilities. Just drop the code snippet as the 'deflate.conf' into the directory as specified and bounce the Apache/httpd instance (i.e. /etc/init.d/httpd restart). However, if you are running Debian or Ubuntu distro, then I recommend looking at this article first (http://www.control-escape.com/web/configuring-apache2-debian.html) for locating where to put the actual deflate.conf file.

For debugging and performance tuning needs, I added a special filter format to the code that allows you to separate what is being compressed and the current ratio of such compression. Just add the following line to your httpd.conf:

CustomLog /path/to/the/deflate_log deflate

For those running virtual hosts, the above CustomLog entry can also be used for each website as well for easier log file reading needs.

NOTE: If you are hosting Drupal or Joomla sites, do not use this deflation configuration in conjunction with the Apache mod_gzip module. It has authenticated and unauthenticated session issues (i.e. it tries to do double compression, thus crushing the site -- just displaying white pages). Drupal actually has a gzip module (http://drupal.org/project/css_gzip) to fix this issue.
<IfModule mod_deflate.c>

SetOutputFilter DEFLATE
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 \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary

 <FilesMatch "\.(js|css|html|xml|x?html?|php)$">
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/atom_xml
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/x-httpd-php
  AddOutputFilterByType DEFLATE application/x-httpd-fastphp
  AddOutputFilterByType DEFLATE application/x-httpd-eruby
  AddOutputFilterByType DEFLATE text/html

DeflateCompressionLevel 5

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate


Open in new window


Featured Post

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Join & Write a Comment

Microsoft Office 365 Backup and Restore Solution by SysTools to export Office 365 mailbox to PST / EML file format on Windows OS. On Mac, tool backup O365 to PST / MBOX / MSG / EML / EMLX file formats. Not only this, restore option helps to import s…
I previously published an Experts Exchange video Micro Tutorial that describes how to scan documents to a PDF file using an excellent, free product called Foxit Reader: How to scan to a PDF file with free software (https://www.experts-exchange.co…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month