开发者

GZIP, Apache, PHP: What should I know to implement in a mature site?

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 ass开发者_如何学Cumption 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!


  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜