8

I was wondering if someone here could help me determine the optimal standard configuration for using mod deflate with Apache. Basically, mod_deflate recommends using the following configuration for getting started right away:

Compress only a few types

AddOutputFilterByType DEFLATE text/html text/plain text/xml http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

However, just from reading the doc, you can customize this for all the browsers out there. In addition, you can customize mod_deflate for all different kinds of mime types. I was wondering if anybody out there has experimented with these settings and found a setting which is optimal for all browsers.

Another example Apache provides, but mention not to use if you don't understand all the config options:

<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

I understand most of the configuration settings, and I would like to setup something similar. I wouldn't mind avoiding compression of images and other media which is already compressed. The details I have problems with, is understand how this reacts with all the different browsers out there, chrome, firefox, IE, Opera, etc... Obviously I'm not concerned with Netscape 4.X. I'm hoping somebody has tested all this already and might be able to recommend a good setting that meets this criteria.

I mean if it's just a matter of using the recommended setting in the doc, I'm fine with that, but I wanted to check just to be sure.

Just to provide a few additional details. We use Apache as a front to all of our webservices. For Example: confluence, git, gitweb, etc...

Tomcat and other services are proxied via Apache so we have configurations for virtualhosts, mod_proxy w/AJP, mod_ssl.

My company doesn't have a dedicated IT team so I have to set much of this up in my spare time. I would appreciate any input you can provide.

So just to state clearly what I'm asking, what is the optimal configuration for handling the basic content needs serving requests from Apache to mainstream browsers?

My list of basic content types so far:

  • text/html
  • text/plain
  • text/xml
  • text/x-js
  • text/javascript
  • text/css
  • application/xml
  • application/xhtml+xml
  • application/x-javascript
  • application/javascript
  • application/json

Types which obviously don't need to be compressed:

  • images - gif, jpg, png
  • archives - exe, gz, zip, sit, rar
Jason Huntley
  • 1,253
  • 3
  • 10
  • 22
  • HTML5 Boilerplate has [some pretty good stuff](https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess). I wouldn't worry about different browsers. All (modern) browsers can handle compression. Also `AddOutputFilterByType` is deprecated. – user123444555621 Jul 18 '12 at 03:50

1 Answers1

8

I went ahead and researched this one out. After reading multiple tutorials and the Apache doc, I was able to piece together something substantial, which seems to work well. Using the list above, I put together a set of rules/declaratives which seem to handle compression with the mainstream content types:

<Location />
# Insert filter
SetOutputFilter DEFLATE

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/x-js text/javascript text/css 
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/x-javascript application/javascript
AddOutputFilterByType DEFLATE application/json

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

# Don't compress images
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

</Location>

For testing, I basically used TamperData for Firefox and turned on deflate logging for apache:

https://addons.mozilla.org/en-US/firefox/addon/tamper-data/

For Apache, add the following:

# For Testing Purposes
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

#LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog logs/deflate_log deflate

http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#deflatefilternote

Jason Huntley
  • 1,253
  • 3
  • 10
  • 22
  • 3
    I don't think I'd even worry about those ancient browsers, but otherwise an excellent write-up. – Chris S Mar 22 '12 at 14:32
  • 2
    `SetOutputFilter DEFLATE` enables compression for everything, so your `AddOutputFilterByType` instructions are not required. – user123444555621 Jul 18 '12 at 03:53
  • Almost true... It's not compressing json data in my case... which is apache used as a load balancer in front of a tomcat cluster. – Philippe Apr 20 '15 at 19:43