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

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 在 gzip 之后会重新计算长度。

我在 Apache Bugtracker 中发现了这个错误https://bz.apache.org/bugzilla/show_bug.cgi?id=51350开发人员说这个错误在 2.4.1 版本中已修复。我安装了 2.4.7 版本,但这个错误仍然存​​在。

如何禁用 204 响应或空主体响应的 gzip 压缩?或者也许有一种方法可以禁用Content-Lengthapache 覆盖标头?

答案1

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

gzip < /dev/null | wc -c
20

相关内容