Apache为gzip压缩204响应返回无效的Content-Length

当apache返回带有204响应代码的gzip压缩响应,而空主体服务器返回无效头Content-Length: 20而不是Content-Length: 0

如果没有gzip压缩(没有Accept-Encoding标头)服务器返回有效的头Content-Length: 0

压缩请求和响应:

 0 % curl -v http://mta.dev/api/wtf/\?id\=09102 --compressed * Hostname was NOT found in DNS cache * Trying 172.17.0.2... * Connected to mta.dev (172.17.0.2) port 80 (#0) > GET /api/wtf/?id=09102 HTTP/1.1 > User-Agent: curl/7.38.0 > Host: mta.dev > Accept: */* > Accept-Encoding: deflate, gzip > < HTTP/1.1 204 No Content < Date: Thu, 09 Jun 2016 15:44:53 GMT * Server Apache/2.4.7 (Ubuntu) is not blacklisted < Server: Apache/2.4.7 (Ubuntu) < X-Powered-By: PHP/5.5.9-1ubuntu4.17 < P3P: policyref="/bitrix/p3p.xml", CP="NON DSP COR CUR ADM DEV PSA PSD OUR UNR BUS UNI COM NAV INT DEM STA" < X-Powered-CMS: Bitrix Site Manager (d04cd2b3dbab106e7537af3767043172) < Set-Cookie: PHPSESSID=8arlnd14t1k97bri56clb2qhh1; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 < Pragma: no-cache < Set-Cookie: BITRIX_SM_GUEST_ID=2328047; expires=Sun, 04-Jun-2017 15:44:53 GMT; Max-Age=31104000; path=/ < Set-Cookie: BITRIX_SM_LAST_VISIT=09.06.2016+18%3A44%3A53; expires=Sun, 04-Jun-2017 15:44:53 GMT; Max-Age=31104000; path=/ < Content-Encoding: gzip < Content-Length: 20 < Content-Type: application/json < * Excess found in a non pipelined read: excess = 20 url = /api/wtf/?id=09102 (zero-length body) * Connection #0 to host mta.dev left intact 

无压缩的请求和响应:

 0 % curl -v http://mta.dev/api/wtf/\?id\=09102 * Hostname was NOT found in DNS cache * Trying 172.17.0.2... * Connected to mta.dev (172.17.0.2) port 80 (#0) > GET /api/wtf/?id=09102 HTTP/1.1 > User-Agent: curl/7.38.0 > Host: mta.dev > Accept: */* > < HTTP/1.1 204 No Content < Date: Thu, 09 Jun 2016 15:38:43 GMT * Server Apache/2.4.7 (Ubuntu) is not blacklisted < Server: Apache/2.4.7 (Ubuntu) < X-Powered-By: PHP/5.5.9-1ubuntu4.17 < P3P: policyref="/bitrix/p3p.xml", CP="NON DSP COR CUR ADM DEV PSA PSD OUR UNR BUS UNI COM NAV INT DEM STA" < X-Powered-CMS: Bitrix Site Manager (d04cd2b3dbab106e7537af3767043172) < Set-Cookie: PHPSESSID=ceqsuv4ie3fkq497uvk6e2gki1; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 < Pragma: no-cache < Set-Cookie: BITRIX_SM_GUEST_ID=2328047; expires=Sun, 04-Jun-2017 15:38:43 GMT; Max-Age=31104000; path=/ < Set-Cookie: BITRIX_SM_LAST_VISIT=09.06.2016+18%3A38%3A43; expires=Sun, 04-Jun-2017 15:38:43 GMT; Max-Age=31104000; path=/ < Content-Length: 0 < Content-Type: application/json < * Connection #0 to host mta.dev left intact 

在PHP应用程序中手动设置Content-Length: 0头文件不起作用,因为apache会在gzipping之后重新计算长度。

我在Apache bugtracker https://bz.apache.org/bugzilla/show_bug.cgi?id=51350中发现了这个bug,开发者说这个bug在2.4.1版本中修复。 我安装了2.4.7版本,并且这个错误仍然会发生。

我如何可以禁用204响应的gzip压缩,或空的身体响应? 或者也许有一种方法来禁用Apache的覆盖Content-Length头?

似乎报告的内容长度是正确的:如果内容为空,则gzipalgorithm将其压缩为20个字节。 尝试在命令行上:

 gzip < /dev/null | wc -c 20