Vary: Origin header (CORS caching)
The Vary header tells caches to store separate versions of responses based on request headers like Origin.
Results in 1–3 minutes · Free · No signup required
Want the result for your own site? Free CORS Checker — paste your domain, get the report.
Why It Matters
Without Vary: Origin, CDNs might cache CORS responses with one origin and serve them for different origins, causing security issues or broken functionality.
How We Check
We verify that responses with dynamic CORS headers include `Vary: Origin` to ensure proper caching behavior.
Good vs Bad Values
| Value | Verdict | Why |
|---|---|---|
Vary: Origin | Good | Tells caches to keep a separate copy for each Origin value, so one site's Access-Control-Allow-Origin is never served to another. |
Vary: Accept-Encoding, Origin | Good | Origin appended to the existing list. Header values are comma separated and the order does not matter. |
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers | Good | The full set for preflight responses, since the allowed methods and headers can depend on what the preflight asked for. Spring and several CORS libraries send this form. |
Access-Control-Allow-Origin: * with no Vary | Good | A static wildcard is identical for every origin, so there is nothing for caches to separate. The scanner skips this case. |
No Access-Control-Allow-Origin header at all | Good | No CORS headers means nothing differs by origin, so Vary: Origin is unnecessary. |
Access-Control-Allow-Origin: https://app.example.com with no Vary header | Bad | If the value depends on the request's Origin, a shared cache can store the answer for one site and replay it to another, blocking legitimate users or granting access to the wrong origin. |
Access-Control-Allow-Origin: https://app.example.com with Vary: Accept-Encoding only | Bad | A Vary header exists but does not list Origin, so cache entries are still shared across origins. |
Vary: * | Weak | Valid HTTP (responses are effectively uncacheable by shared caches), but the scanner does not recognize it as Origin, and it disables caching for everything. |
Access-Control-Allow-Origin set on every response to one fixed origin, no Vary | Weak | Technically consistent, because every cached copy is identical. The scanner still flags it, and adding Vary: Origin costs nothing. |
How to Fix
Add Vary: Origin to any response that sets Access-Control-Allow-Origin dynamically based on the request.
Express: the cors middleware sets it for you when the origin is dynamic. For hand-written headers, append rather than overwrite:
res.setHeader("Access-Control-Allow-Origin", origin);
res.vary("Origin");
Nginx: add_header adds a new header line rather than editing an existing one, which is legal for Vary (caches combine the lines), but check the final response if the upstream already sends its own:
add_header Vary Origin always;
Apache (requires mod_headers) — merge appends the value without duplicating it:
Header always merge Vary Origin
Next.js route handlers:
const headers = new Headers({
"Access-Control-Allow-Origin": allowedOrigin,
"Vary": "Origin",
});
For static headers in next.config.js, add { key: "Vary", value: "Origin" } next to the CORS entries. Cloudflare: add Origin to the cache key (Cache Rules → Custom cache key, available on higher plans), or bypass the cache for CORS endpoints, because the default cache does not vary on it.
Verify with two requests that differ only in Origin: curl -si -H 'Origin: https://app.example.com' https://api.example.com/items and the same with https://other.example. The Access-Control-Allow-Origin value should change or disappear, and both responses should show vary: Origin.
Common Mistakes
- Reflecting the request Origin or choosing one from an allowlist, and sending no `Vary: Origin`. This is the classic CORS cache-poisoning setup.
- Replacing the Vary header instead of appending. `res.setHeader("Vary", "Origin")` in Express wipes a `Vary: Accept-Encoding` set earlier; append with `res.vary("Origin")` or Apache's `Header merge Vary Origin`.
- Sending it only when an Origin header is present. A request without Origin gets a response that can be cached and then served to a cross-origin request, so send it on every response where CORS headers are conditional.
- Assuming every CDN honours Vary. Some, including Cloudflare's default cache, do not key on arbitrary request headers; add Origin to the cache key where the CDN supports it, or mark CORS responses `Cache-Control: private` or `no-store`.
- Forgetting preflight responses and error responses; they too carry or omit CORS headers depending on the origin.
- Adding the header at the proxy while the application sets different CORS headers, producing responses where the two disagree.
Frequently Asked Questions
What is Vary: Origin header (CORS caching)?
The Vary header tells caches to store separate versions of responses based on request headers like Origin.
Why does Vary: Origin header (CORS caching) matter for website security?
Without Vary: Origin, CDNs might cache CORS responses with one origin and serve them for different origins, causing security issues or broken functionality.
How do I fix Vary: Origin header (CORS caching) issues?
Add Vary: Origin to any response that sets Access-Control-Allow-Origin dynamically based on the request.
Related Security Checks
Related Tool
CORS Checker
Run all 6 related checks with our free cors checker
Check Your Website Now
Run a free security scan to check for Vary: Origin header (CORS caching) issues and 61 other security checks.
Scan Your Website Free