带 gzip 支持的预冲洗头标签

发布于 2024-12-03 11:48:19 字数 275 浏览 2 评论 0原文

http://developer.yahoo.com/performance/rules.html

给出了最好预先冲洗头部标签。

但我有一个问题,使用 gzip 时会有帮助吗? (我使用的是apache2)。 我认为完整的文档将一次性被 gzip 压缩,然后发送给客户。

或者是否也可以使用 gzip 以及预刷新 head 标签

http://developer.yahoo.com/performance/rules.html

There it is given it is good to preflush the head tag .

But I have a question will it help while using gzip ? (I am using apache2).
I think full document will get gziped at one shot and then send to the client.

or is it also possible to have gzip as well as pre-flush the head tag

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

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

发布评论

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

评论(1

脸赞 2024-12-10 11:48:19

已编辑

这个问题的原始版本表明我们正在处理 HTTP 标头而不是 HTML 文档中的 部分。我将在下面留下我的原始答案,但它实际上与这个具体问题无关。

要回答有关预刷新文档的 部分的问题 - 虽然可以与 gzip 结合执行此操作,但如果不对 gzip 进行更精细的控制,这可能是不可能的比 Apache 提供的进程。可以将 gzip 压缩流分成可以自行解压缩的块(参见此< /a>)但是如果有一种方法可以将 Apache 的 gzip 实现控制到这样的程度,那么我不知道。

这样做可能会降低 gzip 的效率,使压缩后的大小更大,并且只有当文档的 特别大(例如大于 10KB)时才值得这样做(这这是我通过阅读 gzip 如何在引擎盖下工作而得出的一个有点武断的值,并且绝对不应该被视为福音)。


与 HTTP 标头相关的原始答案:

纯粹从 HTTP 协议的角度来看,而不是确切地如何在基于 Apache 的服务器上实现它,我看不出有任何理由不能预刷新标头并使用gzip 压缩主体。请记住,标头永远不会被 gzip 压缩(如果是的话,客户端如何知道它们已经被压缩过?),内容的传输编码应该不会影响您发送标头的时间。

然而,有几点需要记住:

  • 一旦发送了标头,您就无法改变对传输编码的想法。因此,如果您发送声明正文将被 gzip 压缩的标头,然后意识到您的正文只有 4 个字节,您仍然必须对其进行 gzip,这实际上会增加正文的大小。这可能不会成为问题,除非您省略 Content-Length: 标头,虽然可能,但这是不好的做法,因为这意味着您无法使用持久连接。这引出了下一点...
  • 在此场景中您无法发送 Content-Length: 标头。这是因为在压缩正文之前您不知道正文的大小,此时它已准备好发送,因此您并没有真正(从服务器的角度)预刷新标头,即使您在开始发送正文之前,请先单独发送它们。
  • 如果您需要很长时间来压缩消息正文(服务器负载缓慢/负载过重、正文非常大等),并且在发送标头之后才开始压缩,则存在以下风险:客户端可能会超时等待其余响应。这完全取决于客户端,但是 HTTP 客户端实现如此之多,以至于不能完全忽视这种可能性。

简而言之,是的,可以这样做,但没有包罗万象的“是,这样做”或“不,不这样做”的答案 - 您是否会这样做取决于每个请求以及请求的性质这是回应。

EDITED

The original version of this question suggested we were dealing with HTTP headers rather than the <head> section on an HTML document. I will leave my original answer below, but it actually has no relevance to this specific question.

To answer the question about pre-flushing the <head> section of a document - while it would be possible to do this in combination with gzip, it is probably not possible without more granular control over the gzip process than Apache affords. It is possible to break a gzipped stream into chunks that can be decompressed on their own (see this) but if there is a way to control Apache's gzip implementation to such a degree then I am not aware of it.

Doing so would likely decrease the efficacy of the gzip, making the compressed size larger, and would only be worth doing when the <head> of a document was particularly large, say, greater than 10KB (this is a somewhat arbitrary value I arrived at by reading about how gzip works under the bonnet, and should definitely not be taken as gospel).


Original answer, relating to the HTTP headers:

Purely from the viewpoint of the HTTP protocol, rather than exactly how you would implement it on an Apache based server, I can't see any reason why you can't preflush the headers and also use gzip to compress the body. Keeping in mind that fact that the headers are never gzipped (if they were, how would the client know they had been?), the transfer encoding of the content should have no effect on when you send the headers.

There are, however a couple of things to keep in mind:

  • Once the headers have been sent, you can't change your mind about the transfer encoding. So if you send the headers which state that the body will be gzipped, then realise that your body is only 4 bytes, your would still have to gzip it anyway, which would actually increase the size of the body. This probably wouldn't be a problem unless you were omitting the Content-Length: header which while possible, is bad practice as it means you cannot use persistent connections. This leads on to the next point...
  • You cannot send a Content-Length: header in this secenario. This is because you don't know what the size of the body is until you have compressed it, by which time it is ready to send, so you are not really (from the server's point of view) preflushing the headers, even if you do send them seperately before you start to send the body.
  • If it takes you a long time time to compress the body of the message (slow/heavily loaded server, very large body etc etc), and you don't start the compression until after you have sent the headers, there is a risk the client may time out waiting for the rest of the response. This depends entirely on the client, but the are so many HTTP client implementations out there that this possibility cannot be totally discounted.

In short, yes it is possible to do it, but there is no catch-all, "Yes, do it" or "No, don't do it" answer - whether you would do it depends on each request and the nature of it's response.

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