php - 发送 cache-control/expires/pragma 与 404 响应 - 现代浏览器有效/理解?

标签 php caching header http-status-code-404 response

我们正在努力减少一些“无意义”的流量。

如果我们发送带有 404 响应的有效缓存控制 header (a-la“做缓存”),现代浏览器是否会缓存所请求的资源不存在?

我们使用基于 php 的自定义 404 处理程序,该处理程序发送时不会缓存由于 session 创建而导致的 header ,因此我们删除了这些 header 。但现在想知道如果我们CERTAIN 的资源无效,我们是否应该发送正确的缓存 header ?

顺便说一句,这些是公共(public)资源,由所有用户通过相同的 uri 访问。

谢谢!

最佳答案

现代浏览器实现了 HTTP Protocol理想情况下接近标准,对于代理服务器也是如此,如果你想对用户和非故意的机器人流量产生影响,你应该考虑到这一点。当前version is 1.1 , 一些摘录:

10.4 Client Error 4xx

The 4xx class of status code is intended for cases in which the client seems to have erred. 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. 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. 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.

更具体地针对 404:

10.4.5 404 Not Found

The server has not found anything matching the Request-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. 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.

404 响应代码到此为止。

你问:

now [we] are wondering if for resources we are CERTAIN are invalid, should we send proper cache headers?

您没有给出任何您想要使用的特定缓存 header ,因此回答您的问题有点不准确。缓存是协议(protocol)规范中它自己的一部分: 13 Caching in HTTP .

通常以下内容应该是您要查找的内容,摘自 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.

因此您可以缓存 404 响应消息并将它们发信号通知缓存。代理和客户端应该处理它。

关于php - 发送 cache-control/expires/pragma 与 404 响应 - 现代浏览器有效/理解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6633230/

相关文章:

php - 我想制作一个互斥的复选框,因此表中一次只能有一条记录

Node.js 快速应用程序缓存/页面加载缓慢(swig 模板引擎)

wpf - 带有标题的多个项目组合框?

php - 阿迪恩错误: [status] => 403 [errorCode] => 010 [message] => Not allowed [errorType] => security

php - 时区正确,但时间不正确 - PHP MySQL

javascript - CreateJS 缓存对象 - 对象现在不会在舞台上设置动画

html - 如何防止背景位置移动/折叠?

c++ - 如何隐藏C++头文件中的函数

php - 使用 MySQL 和 PHP PDO 发出基于 SELECT 语句的运行查询

javascript - nginx : deactivate cache for html single page app