防止 Apache 对 gzip 内容进行分块

发布于 2024-10-09 11:06:12 字数 263 浏览 0 评论 0原文

当在 Apache2 中使用 mod_deflate 时,Apache 将对 gzip 内容进行分块,设置 Transfer-encoding: chunked 标头。虽然这会加快下载时间,但我无法显示进度条。

如果我自己在 PHP 中处理压缩,我可以先将其完全 gzip 并设置 Content-length 标头,以便我可以向用户显示进度条。

是否有任何设置可以更改 Apache 的默认行为,并让 Apache 设置内容长度标头而不是对响应进行分块,以便我不必自己处理压缩?

When using mod_deflate in Apache2, Apache will chunk gzipped content, setting the Transfer-encoding: chunked header. While this results in a faster download time, I cannot display a progress bar.

If I handle the compression myself in PHP, I can gzip it completely first and set the Content-length header, so that I can display a progress bar to the user.

Is there any setting that would change Apache's default behavior, and have Apache set a Content-length header instead of chunking the response, so that I don't have to handle the compression myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2024-10-16 11:06:12

您可以使用 sendBufferSize 来获得足够大的值将您的回复包含在一大块中。

那么分块内容就是HTTP/1.1协议的一部分,您可以通过设置 force-response-1.0 强制 HTTP/1.0 响应(因此不会分块:“服务器不得向 HTTP/1.0 客户端发送传输编码。”) 在你的 apache 配置中。但是PHP破坏了这个设置,这是PHP的一个长期已知的错误,有一个解决方法

我们可以尝试在客户端修改请求,使用标头来防止分块内容,但 w3c 表示:“所有 HTTP/1.1 应用程序必须能够接收和解码“分块”传输编码 ”,所以我认为没有像“Accept”这样的标头可以阻止服务器对内容进行分块。但是,您可以尝试在 HTTP/1.0 中设置您的请求,它实际上并不是请求的标头,它是第一行,当然应该可以使用 jQuery。

最后一件事,HTTP/1.0 缺少一件大事,“主机”标头不是强制性的,如果您使用基于名称的虚拟主机,请验证 HTTP/1.0 中的请求是否仍在使用“主机”标头。

编辑:通过使用解决方法中引用的技术,您可以看到可以在 PHP 代码中调整 Apache env。这可用于仅对您的特殊 gzip 压缩内容强制使用 1.0 模式,并且您应该使用它来防止在 HTTP/1.0 中完成应用程序(或使用请求模式为您的 gzip 请求设置 HTTP/1.0)。

You could maybe play with the sendBufferSize to get a value big enough to contain your response in one chunk.

Then chunked content is part of the HTTP/1.1 protocol, you could force an HTTP/1.0 response (so not chunked: “A server MUST NOT send transfer-codings to an HTTP/1.0 client.”) by setting the force-response-1.0 in your apache configuration. But PHP breaks this settings, it's a long-known-bug of PHP, there's a workaround.

We could try to modify the request on the client side with an header preventing the chunked content, but w3c says: "All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding", so I don't think there's any header like 'Accept' and such which can prevent the server from chunking content. You could however try to set your request in HTTP/1.0, it's not really an header of the request, it's the first line, should be possible with jQuery, certainly.

Last thing, HTTP/1.0 lacks one big thing, the 'host' headers is not mandatory, verify your requests in HTTP/1.0 are still using the 'host' header if you work with name based virtualhosts.

edit: by using the technique cited in the workaround you can see that you could tweak Apache env in the PHP code. This can be used to force the 1.0 mode only for your special gzipped content, and you should use it to prevent having you complete application in HTTP/1.0 (or use the request mode to set the HTTP/1.0 for you gzip requests).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文