CORS preflight (OPTIONS) passes on localhost but fails when routed through Nginx reverse proxy — why?
Environment
Backend API (Express) responds correctly to OPTIONS with Access-Control-Allow-* headers when called directly. Nginx 1.18 used as TLS-terminating reverse proxy, proxy_pass to upstream. Clients are browsers sending Authorization header and custom Content-Type.
Symptoms
Browser blocks requests with CORS error 'preflight response missing Allow header' or console shows status 403/405 for OPTIONS when routed through Nginx. Direct curl to backend OPTIONS returns 200 with CORS headers.
Observations
curl -i -X OPTIONS https://backend.example/api shows Access-Control-Allow-Origin and other headers. curl -i -X OPTIONS https://proxy.example/api returns 404 or 204 without Access-Control-Allow-Origin depending on Nginx config. Nginx logs show request hitting proxy but some error logs on OPTIONS if client_cert or auth configured.
Already attempted
Added add_header Access-Control-Allow-Origin * in Nginx server block — but still missing when proxy_pass returns 204. Tried proxy_set_header to forward Authorization; removed authentication on upstream but preflight still fails behind proxy.
Diagnostic steps
- From a client, run: curl -i -X OPTIONS 'https://proxy.example/api' -H 'Origin: https://app.example' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: Content-Type, Authorization'.
- What the result means: If response lacks Access-Control-Allow-* headers, proxy is not returning them. If response is 404/403 with HTML body, Nginx likely intercepted OPTIONS before proxying.
- Check Nginx config for 'limit_except', 'if ($request_method = OPTIONS)', 'auth_request', or error_page for the location; temporarily add a debug location to log $request_method and $upstream_response_time.
- What the result means: If Nginx has clauses that handle OPTIONS, those will be triggered and you must adjust them. Seeing upstream_response_time absent suggests Nginx did not proxy to backend.
- In Nginx location, add or modify add_header directives to include 'always': e.g., add_header Access-Control-Allow-Origin $http_origin always; then reload Nginx and repeat the curl test.
- What the result means: If headers appear after adding 'always', the previous lack was due to add_header only applying to successful responses; verify that this is safe and does not leak headers for error pages.
- Temporarily change Nginx to proxy OPTIONS explicitly: location /api/ { if ($request_method = OPTIONS) { proxy_pass http://upstream; break; } } and test, or create an OPTIONS passthrough using proxy_method if available; test with curl.
- What the result means: If passthrough returns expected CORS headers, earlier config was intercepting OPTIONS. Roll back changes that break other logic and implement a controlled passthrough.
- Inspect Nginx access and error logs while reproducing the failing OPTIONS request (tail -f) and note which upstream (or internal) response code and log lines appear.
- What the result means: If logs show internal 401 or redirect, upstream authentication or a missing header (Origin) caused different behavior. If logs show no upstream hit, Nginx handled the request.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
What exact Nginx behaviors cause preflight to succeed locally but fail through proxy, and which targeted config checks or temporary changes will reveal the issue without breaking production traffic?
Verified community troubleshooting
First verify whether Nginx is proxying the OPTIONS request or handling it itself. From a workstation run curl with Origin and Access-Control-Request-* headers against the proxy and check logs. If Nginx returns a 204/404 without CORS headers but the backend returns proper headers when hit directly, the issue is either add_header without 'always' or Nginx intercepts OPTIONS (limit_except/auth_request). Temporarily add 'add_header ... always' and enable explicit OPTIONS proxying to confirm which change corrects the preflight — then tighten origin rules before promoting to production. These steps provide verifiable evidence of the proxy’s role in the failure.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development31% match
Why does the browser show a different TLS certificate when proxying through my Nginx reverse proxy?
Open1 answer - Web Development25% match
NGINX returns 504 Gateway Timeout for long-running uploads through a Node backend — how to diagnose?
Open1 answer - Networking25% match
WebSocket closes after fixed idle period when routed through Nginx reverse proxy
Open1 answer - Web Development24% match
Nginx returns 504 Gateway Timeout only for backend requests over ~45s to my Node API
Open1 answer - Networking23% match
Chunked Transfer-Encoding uploads fail with 413 only through TLS-terminating NGINX proxy
Open0 answers