FULL AI ANALYSIS

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

Generated Nov 6, 2026, 9:29:48 AM

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

Possible 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%

proxy_pass removes or fails to forward required request headers (Origin/Access-Control-Request-Method) so upstream cannot reply correctly

50%

Suggested fixes

1

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.

2

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.

3

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.

4

Diagnostic step 4

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. Result meaning: If passthrough returns expected CORS headers, earlier config was intercepting OPTIONS. Roll back changes that break other logic and implement a controlled passthrough.

5

Diagnostic step 5

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. Result meaning: 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.