PHP zlib.output_compression fails to set Content-Encoding

This is what the PHP docs say about the zlib.output_compression INI directive:

Whether to transparently compress pages. If this option is set to “On” in php.ini or the Apache configuration, pages are compressed if the browser sends an “Accept-Encoding: gzip” or “deflate” header. “Content-Encoding: gzip” (respectively “deflate”) and “Vary: Accept-Encoding” headers are added to the output. In runtime, it can be set only before sending any output.

Seems pretty straightforward, except that enabling this directive on an openSUSE 10.3 server (and an openSUSE 10.2 server) resulted in a bunch of gibberish as output. Meanwhile, the compression worked just fine on my MacPorts-enhanced MacBook.

After flailing around on Google and php.ini, I eventually eliminated SSL and the firewall as reasons for the problem. During this process I wasted a good amount of time on this bug. Basically, the docs say that you can set zlib.output_compression in your script at runtime, but in reality it doesn’t work.

I finally got down to business with Live HTTP Headers and figured out that enabling zlib compression on my openSUSE servers certainly compressed the content, but did not send the requisite “Content-Encoding: gzip” response header. Sending the header manually within my script (e.g. header("Content-Encoding: gzip");) would correctly turn the gibberish into uncompressed form.

Having already spent hours mucking around with this, I went with a quick and dirty solution:

ob_start('ob_gzhandler');
echo $page->toHtml();
ob_end_flush();

I hunted around Google for this problem, but only found PHP4 references from years ago. Maybe it’s time to ditch openSUSE on my servers… Ubuntu LTS might hit the spot.

This entry was posted in HOW-TOs, Linux, Rants. Bookmark the permalink.

4 Responses to PHP zlib.output_compression fails to set Content-Encoding

  1. Eric says:

    I had a similar problem just now on my website (running on Ubuntu, I might add): setting zlib.output_compression to “On” in my .htaccess file resulted in gibberish. Now I still don’t understand why, but if I set zlib.output_compression to an explicit buffer size instead (e.g., the default 4096), then the Content-Encoding does get set properly, and all appears to be well. I wonder if you might find the same.

  2. theoden says:

    I appreciate the tip, Eric; unfortunately this didn’t work for me. If this is still a problem when I upgrade to the next openSUSE version, then maybe I’ll hammer my head on it some more.

  3. Pingback: Errors while using zlib.output_compression with ini_set

  4. Pingback: zlib.output_compression with ini_set doesn’t work as expected

Leave a Reply

Your email address will not be published.