问题
I have this minimal code that outputs some text:
<?php
$output = "";
for ($i = 0; $i < 7000; $i++) {
$output .= ($i % 2) ? "Foo " : "Bar ";
}
header("Content-Length: ".strlen($output));
echo $output;
exit;
Using Apache 2.2.34 on my web server, and I'm unable to compress the output via .htaccess (see below).
But if I simply remove this header("Content-Length…
line, suddenly the output is compressed as expected and the appropriate headers are sent (Content-Encoding: gzip
, Vary: Accept-Encoding
, Transfer-Encoding: chunked
).
My .htaccess uses the AddOutputFilterByType
and Filter
* directives, I’ve tried both independently as well:
AddOutputFilterByType DEFLATE "text/html"
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
I'm not experiencing this issue in my local working environment, where I'm using Apache 2.4 (sadly I can't upgrade the server).
EDIT: In my case, the problem is solved, see my comments below.
来源:https://stackoverflow.com/questions/53314451/apache-output-compression-doesnt-work-when-content-length-is-set