我想要与 304 响应一起发送哪些标头?

发布于 2024-07-15 08:42:44 字数 598 浏览 10 评论 0原文

当我发送 304 响应时。 浏览器将如何解释我与 304 一起发送的其他标头?

例如,

header("HTTP/1.1 304 Not Modified");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");

这是否可以确保浏览器在 $offset 时间“用完”之前不会发送另一个条件 GET 请求(也不会发送任何请求)?

另外,其他标题呢?

我是否应该将这样的标头与 304: 一起发送:

header('Content-Type: text/html');

我是否必须发送:

header("Last-Modified:" . $modified);
header('Etag: ' . $etag);

以确保浏览器在下次 $offset“用完”时发送有条件的 GET 请求,或者只是保存旧的 Last Modified 和 Etag 值?

发送 304 响应标头时还需要注意其他事项吗?

When I send a 304 response. How will the browser interpret other headers which I send together with the 304?

E.g.

header("HTTP/1.1 304 Not Modified");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");

Will this make sure the browser will not send another conditional GET request (nor any request) until $offset time has "run out"?

Also, what about other headers?

Should I send headers like this together with the 304:

header('Content-Type: text/html');

Do I have to send:

header("Last-Modified:" . $modified);
header('Etag: ' . $etag);

To make sure the browser sends a conditional GET request the next time the $offset has "run out" or does it simply save the old Last Modified and Etag values?

Are there other things I should be aware about when sending a 304 response header?

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

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

发布评论

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

评论(2

℡Ms空城旧梦 2024-07-22 08:42:45

这篇博客文章帮助我驯服了“条件获取”野兽。

一个有趣的摘录(与本的回答部分矛盾)指出:

如果正常响应包含 ETag 标头,则该标头也必须包含在 304 响应中。

缓存标头(Expires、Cache-Control 和/或 Vary),如果它们的值可能与先前响应中发送的值不同。

这完全符合 RFC 2616 sec 10.3。 5.


低于 200 请求……

HTTP/1.1 200 OK
Server: nginx/0.8.52
Date: Thu, 18 Nov 2010 16:04:38 GMT
Content-Type: image/png
Last-Modified: Thu, 15 Oct 2009 02:04:11 GMT
Expires: Thu, 31 Dec 2010 02:04:11 GMT
Cache-Control: max-age=315360000
Accept-Ranges: bytes
Content-Length: 6394
Via: 1.1 proxyIR.my.corporate.proxy.name:8080 (IronPort-WSA/6.3.3-015)
Connection: keep-alive
Proxy-Connection: keep-alive
X-Junk: xxxxxxxxxxxxxxxx

及其最佳有效的 304 对应项。

HTTP/1.1 304 Not Modified
Server: nginx/0.8.52
Date: Thu, 18 Nov 2010 16:10:35 GMT
Expires: Thu, 31 Dec 2011 16:10:35 GMT
Cache-Control: max-age=315360000
Via: 1.1 proxyIR.my.corporate.proxy.name:8080 (IronPort-WSA/6.3.3-015)
Connection: keep-alive
Proxy-Connection: keep-alive
X-Junk: xxxxxxxxxxx

请注意,根据RFC-2616 14.21

This blog post helped me a lot in order to tame the "conditional get" beast.

An interesting excerpt (which partially contradicts Ben's answer) states that:

If a normal response would have included an ETag header, that header must also be included in the 304 response.

Cache headers (Expires, Cache-Control, and/or Vary), if their values might differ from those sent in a previous response.

This is in complete accordance with the RFC 2616 sec 10.3.5.


Below a 200 request...

HTTP/1.1 200 OK
Server: nginx/0.8.52
Date: Thu, 18 Nov 2010 16:04:38 GMT
Content-Type: image/png
Last-Modified: Thu, 15 Oct 2009 02:04:11 GMT
Expires: Thu, 31 Dec 2010 02:04:11 GMT
Cache-Control: max-age=315360000
Accept-Ranges: bytes
Content-Length: 6394
Via: 1.1 proxyIR.my.corporate.proxy.name:8080 (IronPort-WSA/6.3.3-015)
Connection: keep-alive
Proxy-Connection: keep-alive
X-Junk: xxxxxxxxxxxxxxxx

...And its optimal valid 304 counterpart.

HTTP/1.1 304 Not Modified
Server: nginx/0.8.52
Date: Thu, 18 Nov 2010 16:10:35 GMT
Expires: Thu, 31 Dec 2011 16:10:35 GMT
Cache-Control: max-age=315360000
Via: 1.1 proxyIR.my.corporate.proxy.name:8080 (IronPort-WSA/6.3.3-015)
Connection: keep-alive
Proxy-Connection: keep-alive
X-Junk: xxxxxxxxxxx

Notice that the Expires header is at most Current Date + One Year as per RFC-2616 14.21.

野心澎湃 2024-07-22 08:42:45

Content-Type 标头仅适用于包含正文的响应。 304 响应包含正文,因此标头不适用。 同样,您不想发送 Last-ModifiedETag 因为 304 响应意味着文档没有更改(因此这两个值都没有更改)标题)。

有关示例,请参阅Anne van Kesteren 撰写的这篇博客文章,检查WordPress' http_modified 函数。 请注意,它返回 Last-ModifiedETag 或 304 响应。

The Content-Type header only applies to responses which contain a body. A 304 response does not contain a body, so that header does not apply. Similarly, you don't want to send Last-Modified or ETag because a 304 response means that the document hasn't changed (and so neither have the values of those two headers).

For an example, see this blog post by Anne van Kesteren examining WordPress' http_modified function. Note that it returns either Last-Modified and ETag or a 304 response.

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