What is the Vary HTTP header, and how does HTTP caching work?
guide // , 4 min read, by Darthwares
The Vary HTTP response header describes which parts of a request message, aside from the method and URL, influenced the content of the response. Including this header ensures that caching systems separately store different versions of a page based on those specified request header fields. Understanding how request headers and response headers control caching behavior helps developers deliver correct resource versions across distributed networks.
On this page // 5 sections
The short version
- The Vary response header lists the request fields that changed the content returned for a resource.
- Caching mechanisms use Vary headers to create distinct cache keys during content negotiation.
- You must send the exact same Vary header on all responses for a given URL, including 304 status codes.
- The No-Vary-Search header allows servers to tell browsers when URL parameter variations can share a single cached response.
What does the Vary HTTP header do?
The HTTP Vary response header tells caches which request headers influenced the returned resource content. MDN Web Docs explains that HTTP provides a uniform interface for interacting with a resource, which is the target of an HTTP request. Hypertext Transfer Protocol (HTTP) is a stateless application-level protocol for distributed, collaborative, hypertext information systems where requests transfer representations of resources.
When a server uses content negotiation, it inspects incoming request header fields to select the right representation metadata and content. Including a Vary header ensures that responses are separately cached based on those listed request fields. Most often, web servers use Vary to create a unique cache key whenever content negotiation determines what content is delivered to a recipient.
How does HTTP caching process web requests?
All HTTP requests made by a browser are first routed to the browser cache to check for a valid cached response. Web.dev notes that fetching resources over the network is both slow and expensive because large responses require many roundtrips between the browser and the server. Caching behavior is controlled by a combination of request headers and response headers.
The outgoing response headers added by your web server are the most critical part of an HTTP caching setup. By default, a cached response stored for one URL is only reused for a request to that exact same URL. Leaving out the Cache-Control response header does not disable HTTP caching on its own, so setting headers accurately is necessary to control resource reuse.
How should you configure Vary and No-Vary-Search headers?
You should use the exact same Vary header value on all responses for a given URL. MDN Web Docs documents that this requirement applies to 304 Not Modified responses as well as the default response. Sending identical Vary header values prevents unexpected cache key mismatches during conditional HTTP requests.
Servers can also provide the No-Vary-Search HTTP response header to define how a URL's query parameters affect cache matching. This header sets rules specifying whether the same URL with different parameters should save as separate browser cache entries. No-Vary-Search lets the server tell the browser whether parameter order matters and which parameters alter the response. This allows the browser to reuse an existing cached resource despite mismatching URL parameters, avoiding unnecessary network fetches when content remains identical.
How do you audit Vary headers and caching configuration?
Auditing your caching setup involves verifying the request headers and response headers that control HTTP cache behavior. Web.dev documents that all HTTP requests are first routed to the browser cache to check if a valid cached response can fulfill the request. The response headers added by the web server to each outgoing response matter the most in controlling this behavior.
Fetching resources over the network is slow and expensive because large responses require many roundtrips between the browser and server. Configuring the Vary header and No-Vary-Search header correctly ensures responses are separately cached when content negotiation is used, or reused when parameter variations yield the same content.
- 01Check outgoing response headers from the web server to ensure a Vary header is included when content negotiation is used.
- 02Confirm that the same Vary header value is included on all responses for a given URL, including 304 Not Modified responses and the default response.
- 03Review URL parameter behavior to see if No-Vary-Search can tell the browser to reuse cached resources when mismatching parameters return the same content.
| Requirement | Header Behavior | Action Grounded in Specifications |
|---|---|---|
| Content negotiation is used for a URL | Responses must be separately cached based on request fields | Include a Vary header listing the request fields that influenced the content. |
| URL returns 304 Not Modified responses | All responses for a given URL must use identical Vary values | Send the exact same Vary header value on 304 Not Modified responses as the default response. |
| URL parameters do not change returned content | Browser reuses cached resources despite mismatching parameters | Add a No-Vary-Search header to specify parameters that do not affect the response. |
Common questions
What is the primary function of the HTTP Vary header?
The HTTP Vary response header describes the parts of the request message, aside from the method and URL, that influenced the content of the response. It ensures responses are separately cached based on those request headers.
Why must the Vary header be the same on 304 responses?
MDN Web Docs states that the same Vary header value should be used on all responses for a given URL, including 304 Not Modified responses. This ensures cache keys remain valid across revalidation requests.
Does omitting the Cache-Control header disable browser caching?
No, web.dev notes that leaving out the Cache-Control response header does not disable HTTP caching. Browsers still check the cache before making network requests.
What does the No-Vary-Search header do?
The No-Vary-Search HTTP response header specifies rules defining how query parameters affect cache matching. It allows browsers to reuse cached resources despite parameter differences when the content is identical.
Primary sources and further reading
This article was researched and written by an AI model and published automatically. It was written from sentences that were checked to appear on the pages below, but a person has not reviewed the text. If something is wrong, tell us at chibivikramathithan@darthwares.com and we will correct it.