Nginx CORS Preflight Config: map $http_origin & add_header always Pitfalls
Configure Nginx reverse-proxy CORS safely using map $http_origin, the always directive on 204/4xx/5xx responses, and Vary: Origin without breaking header inheritance.
Harden your live domain headers, OpenAPI schemas & AEO discovery
Test your deployed domain's security headers, HSTS, CSP & DMARC live on ReadyCheck, diff OpenAPI contracts & OWASP API Top 10 risks in SpecDiff, or deploy all 9 production configs in minutes with the $49 ReadyCheck Fix Pack.
Harden your live domain headers, OpenAPI schemas & AEO discovery
Test your deployed domain's security headers, HSTS, CSP & DMARC live on ReadyCheck, diff OpenAPI contracts & OWASP API Top 10 risks in SpecDiff, or deploy all 9 production configs in minutes with the $49 ReadyCheck Fix Pack.
Avoid Duplicate Header Collisions with Upstream App
nginx-dedup.conf
# If your upstream Node/Python app ALSO emits CORS headers, hide upstream headers
# in Nginx so browsers do not reject duplicate Access-Control-Allow-Origin headers:
proxy_hide_header Access-Control-Allow-Origin;
proxy_hide_header Access-Control-Allow-Credentials;
proxy_hide_header Access-Control-Allow-Methods;
proxy_hide_header Access-Control-Allow-Headers;
Key Engineering Takeaways
Nginx's add_header directive silently drops headers on HTTP 204, 400, 401, 403, and 500 responses unless the always keyword is appended.
Using map $http_origin $cors_origin returns an empty string for unlisted origins, causing Nginx to omit Access-Control-Allow-Origin cleanly (because Nginx ignores add_header when the value evaluates to empty).
If both Nginx and an upstream Express/FastAPI backend emit Access-Control-Allow-Origin, browsers fail immediately due to multiple Access-Control-Allow-Origin header values.
Common Production Pitfalls to Avoid
Writing add_header in a child if () block causes Nginx to discard EVERY add_header inherited from the parent location/server block (IfIsEvil quirk).
Forgetting proxy_hide_header when migrating CORS from backend middleware to Nginx creates duplicate comma-joined Allow-Origin headers.
Using regex ~* example\.com in map without ^https:// anchors lets https://evil-example.com pass.
Implement dynamic multi-origin CORS with Vary: Origin in Next.js App Router route handlers and middleware.ts without the single-origin limitation of static next.config.mjs headers().
Verify GitHub X-Hub-Signature-256 and Slack v0= webhook signatures using constant-time crypto.timingSafeEqual, raw request bytes, and timestamp replay protection.
Implement production-grade billing webhook HMAC-SHA256 verification for Stripe (`t=<ts>,v1=<hex>`) and Creem (`creem-signature: <hex>`) with raw-body preservation and replay checks.
Why does Nginx return 204 OPTIONS with zero CORS headers?
By default Nginx only runs add_header on 200, 201, 204 (not always in older setups), 206, 301-308, or when an inner if block overrides parent add_header directives. Always specify add_header ... always directly inside the OPTIONS branch or use OpenResty/NJS.
What happens if both Nginx and FastAPI send Access-Control-Allow-Origin?
Browsers abort the fetch with 'The Access-Control-Allow-Origin header contains multiple values'. Set CORS in one layer only, or add proxy_hide_header in Nginx.
Can I verify my live Nginx security + CORS headers together?
Use /security-headers/nginx/ to harden HSTS/CSP alongside Nginx CORS, then scan your live endpoint on audit.runglab.dev.