<

Go Premium for a chance to win a PS4. Enter to Win

x

Global deflate.conf for Apache

Published on
10,500 Points
4,500 Views
Last Modified:
Michael Worsham
Creative problem solver with ability to work either individually or through multiple teams partners for accomplishing goals.
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
Comment
0 Comments

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month10 days, left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month