by Yan Sheng

Rule 4: Gzip Components

It reduces response times by reducing the size of the HTTP response.

gzip and deflate

It's worthwhile to gzip your scripts and stylesheets, but many web sites miss this opportunity(in fact, it's worthwhile to compress any text response including XML and JSON). Image and PDF files should not be gzipped because they are already compressed.

There is a cost to gzipping: it takes additional CPU cycles on the server to carry out the compression and on the client to decompress the gzipped file. To determine whether the benefits outweigh the costs you would have to consider the size of the response, the bandwidth of the connection, and the Internet distance between the client and the server. This information isn't generally available, and even if it were, there would be too many variables to take into consideration. It's worth gzipping any file greater than 1 or 2K. The mod_gzip_minimum_file_size directive controls the minimum file size you're willing to compress. The default value is 500 bytes.

Gzipping generally reduces the response size by about 70%.

Apache 1.3 users mod_gzip while Apache 2.x uses mod_deflate. 相关配置可见apache文档

Proxy Caching Vary: Accept-Encoding

Edge Cases 主要讲当浏览器不支持gzip时, 比如说早期的浏览器版本. The alternative serving compressed content to a browser that can't support it, is far worse. Using mod_gzip in Apache 1.3, a browser whitelist is specified using mod_gzip_item_include with the appropriate User-Agent values: mod_gzip_item_include reqheader "User-Agent: MSIE [6-9]" mod_gzip_item_include reqheader "User-Agent: Mozilla/[5-9]"

In Apache 2.x use the BrowserMatch directive: BrowserMatch ^MSIE [6-9] gzip BrowserMatch ^Mozilla/[5-9] gzip

Adding proxy caches, like Vary: Accept-Encoding,User-Agent


In Action

Nothing Gzipped: http://stevesouders.com/hpws/nogzip.html HTML Gzipped: http://stevesouders.com/hpws/gzip-html.html Everything Gzipped: http://stevesouders.com/hpws/gzip-all.html