Community Pick: Many members of our community have endorsed this article.

Global deflate.conf for Apache

Michael WorshamCloud/Infrastructure Solutions Architect
CERTIFIED EXPERT
Well-rounded and highly experienced with a professional background in cloud/infrastructure solutions and project management.
Published:
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
 </FilesMatch>

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

</IfModule>

Open in new window

0
5,251 Views
Michael WorshamCloud/Infrastructure Solutions Architect
CERTIFIED EXPERT
Well-rounded and highly experienced with a professional background in cloud/infrastructure solutions and project management.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community