The Vary response header is used for indicating to browsers and other user agents which request header values to check when deciding whether to load content from a cache or to instead fetch a new response over the network.
The Vary header is defined in RFC 7231 http://httpwg.org/specs/rfc7231.html#header.vary. Its value is a list of header names. Here’s a simple example of how it works:
- Code running in a browser at
https://origin-a.commakes a request tohttps://some-server.com/some/pathwith the request headerOrigin: https://origin-a.com. - The
https://some-server.comserver sends back the response with the content from/some/pathand the response headerVary: Origin. - A browser receives the response and caches the content from
/some/path. - Code running in the browser at
https://origin-b.commakes a request tohttps://some-server.com/some/pathwith the request headerOrigin: https://origin-b.com. - The browser sees that the
Originrequest-header value for the code fromhttps://origin-b.comis different from that ofhttps://origin-a.com, so instead of using the/some/pathcontent it cached previously, the browser fetches a new response fromhttps://some-server.com/some/pathover the network.
For more details, see the following: