Why use deflate instead of gzip for text files served by Apache?
The simple answer is don't.
RFC 2616 defines deflate as:
deflate The "zlib" format defined in RFC 1950 in combination with the "deflate" compression mechanism described in RFC 1951
The zlib format is defined in RFC 1950 as :
0 1
+---+---+
|CMF|FLG| (more-->)
+---+---+
0 1 2 3
+---+---+---+---+
| DICTID | (more-->)
+---+---+---+---+
+=====================+---+---+---+---+
|...compressed data...| ADLER32 |
+=====================+---+---+---+---+
So, a few headers and an ADLER32 checksum
RFC 2616 defines gzip as:
gzip An encoding format produced by the file compression program
"gzip" (GNU zip) as described in RFC 1952 [25]. This format is a
Lempel-Ziv coding (LZ77) with a 32 bit CRC.
RFC 1952 defines the compressed data as:
The format presently uses the DEFLATE method of compression but can be easily extended to use other compression methods.
CRC-32 is slower than ADLER32
Compared to a cyclic redundancy check of the same length, it trades reliability for speed (preferring the latter).
So ... we have 2 compression mechanisms that use the same algorithm for compression, but a different algorithm for headers and checksum.
Now, the underlying TCP packets are already pretty reliable, so the issue here is not Adler 32 vs CRC-32 that GZIP uses.
Turns out many browsers over the years implemented an incorrect deflate algorithm. Instead of expecting the zlib header in RFC 1950 they simply expected the compressed payload. Similarly various web servers made the same mistake.
So, over the years browsers started implementing a fuzzy logic deflate implementation, they try for zlib header and adler checksum, if that fails they try for payload.
The result of having complex logic like that is that it is often broken. Verve Studio have a user contributed test section that show how bad the situation is.
For example: deflate works in Safari 4.0 but is broken in Safari 5.1, it also always has issues on IE.
So, best thing to do is avoid deflate altogether, the minor speed boost (due to adler 32) is not worth the risk of broken payloads.
That's not really the answer you might expect, but did you try not compressing out data with PHP, but with Apache ?
This can be done use mod_deflate
.
Couple of things :
- it would mean less calculations for PHP
- it would also mean (and that's the nicest part ;-) ) that your CSS/JS files could be compresses to ; considering the size of those, nowadays, it might not be a bad thing to compress them...
As a sidenote : to help us figure out what's going wrong with the compression by PHP, could you give more informations ? Like the URL of your site (if it's public), and some code ?
EDIT now that we have the URL : http://ego.pestaa.hu/
When I go to that page, the response's headers are as follow :
HTTP/1.x 200 OK
Date: Sat, 01 Aug 2009 21:53:37 GMT
Server: Apache
X-Powered-By: PHP/5.2.6
**Content-Encoding: gzip**
Vary: Accept-Encoding
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
(using firefox 3.5 with LiveHTTPHeaders extension)
The three testing sites you indicated also say that your page is gzipped.
Did you find what the problem was ?
Are you sure this is not something coming from your browser, that would not send the following header in the request :
Accept-Encoding: gzip,deflate
EDIT after other answer
Quote from the comments :
the source of problem probably isn't
my website but my isp/browser/router.
One of them may decompress every
component before it reaches internal
processes.
Oh, if the problem is not your website, I think I totally mis-understood the question -- sorry about that :-( I thought you didn't know how to server gzipped content from your website.
And I think I'm not the only one, btw
If the problem has nothing to do with your website's configuration, then, two possibilities :
- maybe a mis-configuration of firefox ? If you go to
about:config
, what does the network.http.accept-encoding
say ? For me, it's gzip,deflate
- if the problem is coming from your ISP or something like that, there's nothing anyone can do...
Best Answer
Did you try these lines in your .htaccess?
It works for my site.