Networking

CORS preflight (OPTIONS) passes on localhost but fails when routed through Nginx reverse proxy — why?

Environment

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

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

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

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

Diagnostic steps

  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

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?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
tildatech Answer
user
Posted 3w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

add_header directives used without the 'always' flag so headers are not emitted for response codes like 204/401/403 80%
Nginx intercepts or handles OPTIONS itself (limit_except, auth_request, or error_page) and does not include CORS headers 75%
Upstream responds with status that Nginx transforms (e.g., 401 redirected to login page) causing browser to see non-CORS response 60%

Summary

Common causes: Nginx does not forward or return CORS headers for responses with code like 204 unless add_header has 'always' flag; OPTIONS may be answered by Nginx itself (e.g., auth_request, limit_except, or error_page) before proxying; proxying may strip or not forward required headers. Verifying whether Nginx handles OPTIONS or passes to upstream, and whether add_header uses 'always', will identify root cause.

Suggested Fixes

Diagnostic step 1
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'. Result meaning: 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.
Diagnostic step 2
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. Result meaning: 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.
Diagnostic step 3
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. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags