my mod_gzip settings (deflate.conf)

Paul Buchheit posted about gzip settings, and I thought I'd post my deflate.conf (apache) because the default apache stuff isn't nearly aggressive enough.

This stuff goes into conf.d/deflate.conf, and it's cribbed from several places on the web. Wish I could credit them, but I forgot.

If you don't use settings like this you'll find that your CSS and JS files don't get compressed, or you'll compress them all the time, even for the browsers that can't handle them, or you'll get your stuff cached by proxies that will serve the files to browsers that can't handle them, etc. I've not done totally exhaustive testing, but this is what I use on all my sites.

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

DeflateCompressionLevel 9

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# IE5.x and IE6 get no gzip, but allow 7+
BrowserMatch \bMSIE\s7 !no-gzip
# IE 6.0 after SP2 has no gzip bugs!
BrowserMatch \bMSIE.*SV !no-gzip
# Sometimes Opera pretends to be IE with "Mozila/4.0"
BrowserMatch \bOpera !no-gzip
Header append Vary User-Agent env=!dont-vary

There's an nginx equivalent around here somewhere, I'll dig it up.

2 comments:

  1. Did you submit this as a bug with Apache? Sounds like the sort of thing that would be useful to other people.

    ReplyDelete
  2. Anonymous8:53 AM

    Very helpful. Thanks for posting!

    Wanted to share that you get diminishing CPU time/compression returns past the default zlib level 6.

    So DeflateCompressionLevel 9 will give you marginally smaller files (in my experience, you save less than half a percent of gzip size when jumping from level 6 to level 9), but it'll cost you significantly more CPU time (2x CPU time from level 6 to level 9, in my experience).

    The end result is that end users wait more due to the added CPU time, resulting in overall slower page loads.

    Generally, level 6 (the default) is a good time/space tradeoff most of the time.

    ReplyDelete