Apache output compression doesn't work when Content-Length is set

别等时光非礼了梦想. 提交于 2019-12-25 03:15:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!