Vary
是一个HTTP响应头部信息,它决定了对于未来的一个请求头,应该用一个缓存的回复(response)还是向源服务器请求一个新的回复。它被服务器用来表明在 content negotiation algorithm(内容协商算法)中选择一个资源代表的时候应该使用哪些头部信息(headers).
在响应状态码为 304
Not Modified
的响应中,也要设置 Vary 首部,而且要与相应的 200
OK
响应设置得一模一样。
Header type | Response header |
---|---|
Forbidden header name | no |
语法
Vary: * Vary: <header-name>, <header-name>, ...
说明
- *
- 所有的请求都被视为唯一并且非缓存的,使用
Cache-Control
: no-store
,来实现则更适用,这样用于说明不存储该对象更加清晰。 - <header-name>
- 逗号分隔的一系列http头部名称,用于确定缓存是否可用。
例子
动态服务
哪种情况下使用 Vary: 对于User-Agent
头部信息,例如你提供给移动端的内容是不同的,可用防止你客户端误使用了用于桌面端的缓存。 并可帮助Google和其他搜索引擎来发现你的移动端版本的页面,同时告知他们不需要Cloaking。
Vary: User-Agent
定义
版本 | 标题 |
---|---|
RFC 7231, section 7.1.4: Vary | Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content |
浏览器兼容性
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.