Expires 和 Cache-Control 标头有什么区别?
Expires
和 Cache-Control
标头之间有什么区别?
What’s the difference between Expires
and Cache-Control
headers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它们都可以用来控制缓存响应的生命周期,但是 HTTP/1.1 中引入的 Cache-Control 提供了更多功能,并且通常应该比 Expires 更受青睐, HTTP/1.0 中定义。
在现代浏览器中,
Cache-Control: max-age
或Cache-Control: s-maxage
指令也会覆盖响应中的任何 Expires 标头。作为一个具体示例,
Cache-Control: max-age=604800
指示应考虑给定 HTTP 响应的秒数新鲜。换句话说,在 HTTP 响应生成后 604800 秒(7 天),响应应被视为陈旧且可从缓存中逐出。Expires
使用显式的日期时间字符串,例如“Friday, 28 Feb 2020 20:20:20 GMT”
,并且显然比简单的数字更难解析(和生成!)秒整数。其他了解 HTTP 缓存工作原理的好资源:
They can both be used to control the lifetime of a cached response but
Cache-Control
, introduced in HTTP/1.1, offers many more features and should generally be favored overExpires
, defined in HTTP/1.0.In modern browsers a
Cache-Control: max-age
orCache-Control: s-maxage
directive will also override any Expires header in the response.As a concrete example,
Cache-Control: max-age=604800
indicates the number of seconds that the given HTTP response should be considered fresh. In other words, the response should be considered stale and evictable from the cache 604800 seconds (7 days) after the HTTP response was generated.Expires
uses explicit datetime strings like"Friday, 28 Feb 2020 20:20:20 GMT"
and is obviously more challenging to parse (and generate!) than a simple number of seconds integer.Other good resources for understanding how HTTP caching works:
如果您使用 CDN(云交付网络),我建议使用 Cache-Control,最大使用时间以秒为单位。例如缓存控制:max-age=604800。
这可以防止您的原始服务器出现请求高峰:通过“Expires Wed, 30 Oct 20xx 04:37:07 GMT”,所有浏览器都会同时请求您。
If you are using a CDN (Cloud Delivery Network) I recommend to use Cache-Control with a max-age time in seconds. For example Cache-Control: max-age=604800.
This prevents request-peaks to your origin-server: With "Expires Wed, 30 Oct 20xx 04:37:07 GMT" all browsers will request you at the same time.
根据这篇 Google 开发者文章,HTTP 缓存:
According to this Google Developers article, HTTP Caching:
Cache-Control 是在 HTTP/1.1 中定义的,告诉从服务器到客户端的所有缓存机制是否可以缓存该对象。它以秒为单位:
Cache-Control: max-age=3600
。Expires
标头字段给出了响应被视为过时的日期/时间。 Expires 值是 HTTP 日期时间戳:Expires: Tue, 18 Jul 2017 16:07:23 GMT
。如果响应包含带有
max-age
指令的Cache-Control
字段,则接收者必须忽略Expires
字段。Cache-Control was defined in HTTP/1.1, tells all caching mechanisms from server to client whether they may cache this object. It is measured in seconds:
Cache-Control: max-age=3600
.The
Expires
header field gives the date/time after which the response is considered stale. The Expires value is an HTTP-date timestamp:Expires: Tue, 18 Jul 2017 16:07:23 GMT
.If a response includes a
Cache-Control
field with themax-age
directive, a recipient MUST ignore theExpires
field.Heroku 开发中心有一篇关于此主题的优秀文章 。
引述其中,
Heroku devcenter has an excellent article on this subject.
Quoting from it,
除了CC的私有/公共选项之外,我看不出有什么区别。当使用诸如“访问加 1 年/月/周/日”之类的 Expires 时,它的工作方式与 CC 完全相同。
Except for the private/public options of CC, I can't see any difference. When using Expires like "access plus 1 year/month/week/day", it works in exactly the same way as CC does.
如果你仍然感兴趣,我直接从谷歌的男孩那里留下这个推荐。
https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
他们更喜欢 Expires before 而不是 Cache-Control
If you are still interested, I leave this recommendation directly from google's boys.
https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
They prefer Expires before than Cache-Control