3

Apache 2.2.17 PHP 5.3.3

Currently, my app doesn't use gzip, but I would like it to. However, I'm not sure of a few things:

  1. I know that IE6 has problems with it; no need to go back over those here.
  2. What benefits/drawbacks are there to implementing this in Apache (mod_deflate) vs PHP? Do they basically produce the same result? I assume Apache would be more efficient, is this a correct assumption?
  3. The app generates quite a few types of responses: pdf, xml, zip, xls, csv, images (see next point), and of course the normal AJAX-type stuff.
  4. I've seen that many examples choose not to gzip images, why is this? Is it due to the assumption that most images are already in a compressed format, or does it have to do with it being binary data? I do create some images dynamically, and serve others statically.
  5. I also stream audio files (wav, mp3) via a PHP mechanism; any gotchas here?

Essentially, if you've implemented gzip on a mature site, and run into problems, I'd love to know what those were, and what you did to work-around these problems. Thanks!

ken
  • 3,413
  • 1
  • 25
  • 37
  • 1
    Related: http://stackoverflow.com/questions/388595/why-use-deflate-instead-of-gzip-for-text-files-served-by-apache – Pekka Nov 02 '10 at 19:02

2 Answers2

5
  1. Nothing else to be said

  2. Do it in Apache, it is easy to set up and requires no change to your code. You can also set it up easily to compress css and js files if you want.

  3. Apache can be set to compress only specific file types

  4. Compressing an already compressed file will almost always result in a larger file. If you have uncompressed images, like bmps, you will get better results using a compression technique specific to images than using gzip, so you are better off just converting them to gif, png, jpg depending on your needs.

  5. I am not sure about streaming, but the same as images applies to audio. If it is already compressed, don't compress it again, and if it is not, use a compression technique specific to the type.

Basically, gzip is great for compressing text files so I would set Apache to compress those. For anything else, there are better solutions if you need it.

Alan Geleynse
  • 23,597
  • 5
  • 43
  • 54
  • Thanks, very insightful. I wasn't aware that double-compressing could have such a negative effect, although it does make sense to use any context-native compression available (like gif vs bmp) in lieu of general gzip compression. – ken Nov 02 '10 at 20:37
  • Its something that a lot of people don't realize, but compression needs a way to go back to the original. That means that it needs to map any possible input into a unique output, and there is mathematically no way it can always go smaller. There will be some inputs that have to be mapped to a larger output. So each compression technique has a specific set of files it works well on, and others it doesn't. – Alan Geleynse Nov 02 '10 at 20:40
1

Other than bmp's (if you see someone using them over the wire, prepare your shotgun), images are mostly already compressed, including jpeg's, png's and gif's.

I use this in my static subdomains, compiled from some online resources (which can be found easily Googling):

# Insert filter
<FilesMatch "(?i)^.*\.(js|css|html?|php3?|xml|txt)$">
    SetOutputFilter DEFLATE
</FilesMatch>
# *.cache.js and *.cache.css files are only checked after 6 month (e.g. jQuery.1.4.2.cache.js)
<FilesMatch "(?i)^.*\.(cache)\.(js|css)$">
    FileETag None
    ExpiresActive On
    ExpiresDefault "access plus 6 month"
</FilesMatch>
#AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript

# 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
# 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

The only special thing I've added is that I cache *.cache.js and *.cache.css files for 6 months, since they rarely change. E.g. jqueryui.cache.css

Halil Özgür
  • 14,749
  • 4
  • 45
  • 55
  • Respectfully, I didn't ask for an implementation. Rather, I'm looking for any potential pitfalls that I might encounter, or things I should be aware of. – ken Nov 02 '10 at 20:35
  • 1
    I see, I could've been a bit more clear. That implementation has worked in many production environments, inluding most -if not all- of the points in your question. Rather than use as-is, I thought it as something one can get ideas off. – Halil Özgür Nov 03 '10 at 07:32