适用于不同类型资源的理想HTTP缓存控制标头 [英] Ideal HTTP cache control headers for different types of resources
问题描述
我想找到一个最小的标题集,可以与所有缓存和浏览器一起使用(当使用 HTTPS 时也是如此!)
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:
示例:0A470E87CC58EE133616F402B5DDFE1C.cache.html(由GWT自动生成)
-
这些文件会自动分配一个新名称,他们改变了内容(基于MD5)。
These files are automatically assigned a new name, when they change content (based on the MD5).
他们应该尽可能地缓存,即使使用HTTPS(所以我假设,我应该设置 Cache-Control:public
,特别是对于Firefox?)
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.
示例:index.html,mymodule.nocache.js
-
当部署新版本的网站时,这些文件会在不更改URL的情况下更改其内容。
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.
示例:JSON响应
- 在任何情况下都不应将这些资源未加密缓存到磁盘。 (除非我有一些可以缓存的特定请求。)
我对每种类型可能会使用哪些标题有一个大致的想法,但总有一些我可能会丢失。
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
- 任何缓存都可以缓存表示。缓存的表示形式将被视为新的1年:
Cache-Control: max-age=31556926
– Representations may be cached by any cache. The cached representation is to be considered fresh for 1 year:
要将响应标记为永不过期,原始服务器将发送
Expires 日期约为发送
后的一年。 HTTP / 1.1服务器不应该在未来发送超过一个
年的 Expires 日期。
Cache-Control:no-cache
- 允许任何缓存缓存表示。但是缓存必须在发布缓存副本之前将请求提交给源服务器进行验证。 Cache-Control:no-store
- 高速缓存不得在任何条件下缓存表示。
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.参见 Mark Nottingham的缓存教程 以获取更多信息。
See Mark Nottingham’s Caching Tutorial for further information.
这篇关于适用于不同类型资源的理想HTTP缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!