17

当我发送 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 响应标头时我还应该注意其他事项吗?

4

2 回答 2

13

为了驯服“条件获取”野兽,这篇博文帮助了我很多。

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

如果正常响应包含 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

请注意,Expires标头最多Current Date + One Year符合RFC-2616 14.21

于 2010-11-19T15:23:13.380 回答
6

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

例如,请参阅Anne van Kesteren 的这篇博客文章检查WordPresshttp_modified功能。请注意,它返回and 或304响应Last-ModifiedETag

于 2009-03-27T20:27:29.970 回答