A misconfigured CORS header behind Cloudflare and Nginx doesn’t behave like it does in a tutorial: the same request can work on a direct connection to the server and fail once routed through the CDN, or work for one visitor and not the next depending on what the cache has stored. This guide covers the failures specific to this exact stack, not the general mechanics of CORS already covered in this guide to cross-origin resource sharing.
Why CORS gets harder with a CDN in front of a reverse proxy
CORS rests on a simple principle: the browser checks that the response carries an Access-Control-Allow-Origin header matching the origin of the page that made the request. The problem, behind Cloudflare and Nginx, is that there are now three places where that header can be added, lost, or altered: Cloudflare’s edge cache, Nginx’s server block, and the application itself. Each can act independently, and none of the three knows what the other two are doing.
Two mechanisms make this specifically worse with a CDN in front of the proxy. First, the response to a preflight OPTIONS request is often cached by Cloudflare like any other HTTP response, even though it should never be cached without accounting for the request’s Origin header. Second, Nginx can end up with a CORS configuration copied from a generic tutorial that adds an Access-Control-Allow-Origin header on top of the one the application already adds, producing a duplicate header that the browser simply rejects.
The most common cause of an intermittent CORS error behind Cloudflare isn’t a misconfiguration — it’s a cached response that ignores which origin actually requested it.
Diagnosing layer by layer
Before touching the configuration, you need to know which of the three layers is at fault. The most reliable method is to query the origin directly, bypassing Cloudflare, then compare with a request going through the CDN:
# preflight request as a browser would send it, bypassing Cloudflare
curl -s -o /dev/null -D - \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: POST" \
-X OPTIONS \
--resolve api.example.com:443:203.0.113.10 \
https://api.example.com/v1/orders
# the same request, this time through Cloudflare
curl -s -o /dev/null -D - \
-H "Origin: https://app.example.com" \
-H "Access-Control-Request-Method: POST" \
-X OPTIONS \
https://api.example.com/v1/ordersIf the first request returns the right headers and the second doesn’t, or returns stale ones, the cause lies in the Cloudflare layer: cache, Page Rule, or Transform Rule. If both fail the same way, the problem is in Nginx or the application itself, and you need to check which of the two is adding, or omitting, the header.
The most common failures and their fix
| Observed symptom | Most likely cause | Fix |
|---|---|---|
| The CORS header is correct on direct access but missing or stale behind Cloudflare | The OPTIONS response was cached without accounting for the Origin header | Exclude OPTIONS requests and API routes from cache via a Cache Rule, or include the Origin header in the cache key |
| “The ‘Access-Control-Allow-Origin’ header contains multiple values” | Nginx and the application each add their own header | Keep a single source of truth: remove it from Nginx if the application already handles it, or the other way around |
Cookies aren’t sent despite withCredentials: true on the client | Access-Control-Allow-Origin is set to * instead of reflecting the exact origin | Reflect the exact requested origin precisely and add Access-Control-Allow-Credentials: true |
| The preflight returns a 405 error | Nginx doesn’t handle the OPTIONS method specifically, so it reaches the backend | Handle OPTIONS directly in Nginx with a 204 status and the expected headers |
| It works for one visitor but not the next on the same route | A response reflecting one origin was cached and served as-is to another origin | Add Vary: Origin to the response and check that Cloudflare respects it for caching |
Fixing it on the Nginx side: a single source of truth
The most robust setup handles the preflight directly in Nginx, without ever duplicating a header the application might already add. If the application already handles CORS itself (for example via a Node middleware, as described in this guide to running Node.js in production behind systemd and Nginx), Nginx should simply pass the response through untouched. If it needs to handle CORS itself because the application doesn’t, here’s a block that reflects the origin rather than using a wildcard, answers the preflight correctly, and signals variation on Origin so upstream caches don’t mix up responses:
map $http_origin $cors_origin {
default "";
"~^https://(app|admin)\.example\.com$" $http_origin;
}
server {
location /v1/ {
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Access-Control-Allow-Credentials "true" always;
add_header Vary "Origin" always;
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
add_header Access-Control-Allow-Headers "Authorization, Content-Type" always;
add_header Content-Length 0;
return 204;
}
proxy_pass http://backend_app;
}
}The map block avoids the * wildcard, which must be avoided as soon as cookies or an Authorization header are involved, while still accepting several legitimate subdomains. The Vary: Origin line is the one most often forgotten, and it’s exactly the one that stops a response cached for one origin from being served as-is to another.
Fixing it on the Cloudflare side: keep API routes out of the default cache
Even with a perfect Nginx setup, Cloudflare can still cache an OPTIONS response or a generic API response if the cache level or a page rule allows it. The most reliable fix is a Cache Rule that explicitly disables caching on API routes and on the OPTIONS method, rather than relying on the origin’s Cache-Control headers to override a more permissive rule set upstream. When caching those routes is genuinely wanted for performance reasons, including the Origin header in the cache key prevents a response reflecting one origin from being served to another.
Key takeaways
Behind Cloudflare and Nginx, a CORS failure is almost never an isolated syntax problem: it’s a problem of layers that don’t coordinate. The method that works is always the same: compare a direct request to the origin with the same request through the CDN, designate a single source of truth for the CORS header, explicitly exclude sensitive routes from cache rather than relying on default headers, and never combine a wildcard with session credentials.
On projects where this diagnosis keeps coming up, the number one cause remains a CDN caching an OPTIONS response, well ahead of Nginx syntax errors. It’s a classic blind spot because each layer’s documentation treats CORS as an isolated topic, never mentioning its interaction with the cache of the layer above it. The habit worth building is simple: isolate the faulty layer with the two curl requests from this guide before touching anything, rather than editing Nginx, the application, and Cloudflare all at once and hoping one of the three changes fixes it. — Simon Janvier
Further reading: the MDN documentation on CORS.
