简体   繁体   English

发送带有 404 响应的缓存控制/过期/杂注 - 现代浏览器是否有效/理解?

[英]sending cache-control / expires / pragma with 404 response - valid / understood by modern browsers?

we are working on reducing some 'pointless' traffic.我们正在努力减少一些“无意义”的流量。

if we send valid cache control headers (a-la "do cache") with a 404 response, will modern browsers cache that the requested resource does not exist?如果我们发送带有 404 响应的有效缓存控制标头(a-la “do cache”),现代浏览器会缓存请求的资源不存在吗?

we use a custom php based 404 handler that was sending do not cache headers due to session creation, so we removed those.我们使用了一个自定义的基于 php 的 404 处理程序,由于 session 创建,它正在发送不缓存标头,因此我们删除了这些。 but now are wondering if for resources we are CERTAIN are invalid, should we send proper cache headers?但是现在想知道我们确定的资源是否无效,我们应该发送正确的缓存头吗?

these are public resources btw accessed by the same uri by all users.这些是所有用户通过相同 uri 访问的公共资源。

thanks!谢谢!

Modern Browsers have an implementation of the HTTP Protocol ideally close to the standard, same for proxy servers which you should take into account if you like to have an effect on user- and non-intentional-bot-traffic.现代浏览器有一个HTTP 协议的实现,理想情况下接近标准,代理服务器也是如此,如果你想对用户和非故意的机器人流量产生影响,你应该考虑到这一点。 The current version is 1.1 , some excerpts:当前版本是 1.1 ,部分摘录:

10.4 Client Error 4xx 10.4 客户端错误 4xx

The 4xx class of status code is intended for cases in which the client seems to have erred.状态码的 4xx class 适用于客户端似乎出错的情况。 Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.除了响应 HEAD 请求时,服务器应该包含一个实体,其中包含对错误情况的解释,以及它是暂时的还是永久的情况。 These status codes are applicable to any request method.这些状态码适用于任何请求方法。 User agents SHOULD display any included entity to the user.用户代理应该向用户显示任何包含的实体。

If the client is sending data, a server implementation using TCP SHOULD be careful to ensure that the client acknowledges receipt of the packet(s) containing the response, before the server closes the input connection.如果客户端正在发送数据,使用 TCP 的服务器实现应该小心确保客户端在服务器关闭输入连接之前确认收到包含响应的数据包。 If the client continues sending data to the server after the close, the server's TCP stack will send a reset packet to the client, which may erase the client's unacknowledged input buffers before they can be read and interpreted by the HTTP application.如果客户端在关闭后继续向服务器发送数据,则服务器的 TCP 堆栈将向客户端发送一个复位数据包,这可能会在 HTTP 应用程序读取和解释之前擦除客户端未确认的输入缓冲区。

More specifically for 404:更具体地说,对于 404:

10.4.5 404 Not Found 10.4.5 404 未找到

The server has not found anything matching the Request-URI.服务器未找到任何与请求 URI 匹配的内容。 No indication is given of whether the condition is temporary or permanent.没有说明这种情况是暂时的还是永久性的。 The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.如果服务器通过一些内部可配置的机制知道旧资源永久不可用并且没有转发地址,则应该使用 410 (Gone) 状态代码。 This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.当服务器不希望确切地揭示请求被拒绝的原因或没有其他响应适用时,通常使用此状态代码。

So much for the 404 response code. 404 响应代码就这么多。

You asked:您问:

now [we] are wondering if for resources we are CERTAIN are invalid, should we send proper cache headers?现在 [我们] 想知道对于我们确定的资源是否无效,我们是否应该发送正确的缓存标头?

You've not given any specific cache headers you'd like to use, so it's a bit unprecise to answer your question.您没有提供任何您想使用的特定缓存标头,因此回答您的问题有点不精确。 Caching is a section of it's own in the protocol specification: 13 Caching in HTTP .缓存是协议规范中它自己的一部分: 13 Caching in HTTP

Generally the following should be what you're looking for, excerpt from 13.1.1 Cache Correctness :通常,以下内容应该是您要查找的内容,摘自13.1.1 Cache Correctness

A correct cache MUST respond to a request with the most up-to-date response held by the cache that is appropriate to the request (see sections 13.2.5, 13.2.6, and 13.12) which meets one of the following conditions:正确的缓存必须使用缓存所持有的最新响应来响应请求,该响应符合以下条件之一:

... ...

 3. It is an appropriate 304 (Not Modified), 305 (Proxy Redirect), or error (4xx or 5xx) response message.

So you can cache 404 response messages and signal them to cache.因此,您可以缓存 404 响应消息并将它们发送到缓存。 Proxies and Clients should handle it.代理和客户应该处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM