浏览器缓存如何处理盗链图像 + 204 响应?

How does browser caching handle hotlinked images + 204 responses?

我在网上看到了一些相互矛盾的答案,我正试图从根本上理解这一点。假设我的网站上有一张图片被热链接(是的,经过许可):

<img src="externalserver.com/catpic.png">
  1. 假设所有相关方都启用了浏览器缓存,将 该图像被缓存(也就是来自外部站点的图像是否可以缓存)?
  2. 如果外部服务器决定在同一文件位置提供不同的图像,比如重写 .htaccess,缓存会被破坏吗?
  3. 如果外部服务器决定使用 ?randomquerystring以后缓存会不会坏掉?
  4. 如果外部服务器改为使用 HTTP 状态代码 204 进行响应 实际提供图像,缓存会发生什么?

谢谢!祝大家节日快乐。

这一切都取决于。

  1. 用户代理可以缓存图像。是否缓存图像取决于服务器如何设置 Cache-ControlExpires header。有关详细信息,请参阅 MDN 上的 Cache-Control header page
  2. 用户代理可能会也可能不会获取新资源。这取决于 Cache-Control header, Expires header, 资源是否陈旧,服务器是否使用验证器以及它是否执行弱或强验证。有关详细信息,请参阅 MDN 上的 HTTP conditional requests page
  3. 服务器不为 ?randomquerystring 提供资源。相反,客户端可以使用 ?randomquerystring 请求资源。 ?randomquerystring 被称为缓存破坏者。用户代理将执行新请求,但我听说某些代理可能会忽略该类型的缓存破坏器并且仍然 return 缓存响应。
  4. 用户代理可能会尊重 Cache-ControlExpires header。相关摘录自RFC 2616

A response received with any other status code (e.g. status codes 302 and 307) MUST NOT be returned in a reply to a subsequent request unless there are cache-control directives or another header(s) that explicitly allow it. For example, these include the following: an Expires header (section 14.21); a "max-age", "s-maxage", "must- revalidate", "proxy-revalidate", "public" or "private" cache-control directive (section 14.9).

有关一般性 HTTP 缓存的更多信息,请查看 Ilya Grigorik 的 HTTP Caching 文章。