问题描述
我想找到一组最小的标头,它们适用于所有"缓存和浏览器(在使用 HTTPS 时也是如此!)
在我的网站上,我将拥有三种资源:
(1) 永久可缓存(公共/对所有用户均等)
这些文件在更改内容时会自动分配一个新名称(基于 MD5).
即使使用 HTTPS,它们也应该尽可能地被缓存(所以我假设,我应该设置
Cache-Control: public
,尤其是对于 Firefox?)如果内容发生了变化,它们不应要求客户端往返于服务器进行验证.
(2) 偶尔更改(公共/所有用户平等)
在部署新版本的网站时,这些文件会更改其内容而不更改 URL.
它们可以被缓存,但可能需要每次来回重新验证.
(3) 每个请求的个体(私人/用户特定)
- 在任何情况下都不应将这些资源未加密地缓存到磁盘中.(也许我会有一些可以缓存的特定请求.)
我大致了解每种类型可能使用哪些标头,但总有一些我可能会遗漏的东西.
我可能会使用这些设置:
Cache-Control: max-age=31556926
– 表示可以被任何缓存缓存.缓存的表示将被视为新鲜 1 年:为了将响应标记为永不过期",源服务器发送一个Expires 日期从响应时间算起大约一年发送.HTTP/1.1 服务器不应发送超过一个 Expires 日期未来一年.
Cache-Control: no-cache
– 表示可以被任何缓存缓存.但是缓存必须在释放缓存副本之前将请求提交给源服务器进行验证.Cache-Control: no-store
– 缓存在任何情况下都不得缓存表示.
有关详细信息,请参阅 Mark Nottingham 的 缓存教程.
I want to find a minimal set of headers, that work with "all" caches and browsers (also when using HTTPS!)
On my web site, I'll have three kinds of resources:
(1) Forever cacheable (public / equal for all users)
These files are automatically assigned a new name, when they change content (based on the MD5).
They should get cached as much as possible, even when using HTTPS (so I assume, I should set
Cache-Control: public
, especially for Firefox?)They shouldn't require the client to make a round-trip to the server to validate, if the content has changed.
(2) Changing occasionally (public / equal for all users)
These files change their content without changing the URL, when a new version of the site is deployed.
They can be cached, but probably need a round-trip to be revalidated every time.
(3) Individual for each request (private / user specific)
- These resources should never be cached unencrypted to disk under no circumstances. (Except maybe I'll have a few specific requests that could be cached.)
I have a general idea on which headers I would probably use for each type, but there's always something I could be missing.
I would probably use these settings:
Cache-Control: max-age=31556926
– Representations may be cached by any cache. The cached representation is to be considered fresh for 1 year:Cache-Control: no-cache
– Representations are allowed to be cached by any cache. But caches must submit the request to the origin server for validation before releasing a cached copy.Cache-Control: no-store
– Caches must not cache the representation under any condition.
See Mark Nottingham’s Caching Tutorial for further information.
这篇关于适用于不同类型资源的理想 HTTP 缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!