Fix: Duplicate CORS Headers from Nginx and Application
Duplicate CORS headers cause browser errors like The 'Access-Control-Allow-Origin' header contains multiple values 'https://app.com, https://app.com'. This happens when both Nginx and your backend application set the same CORS headers independently.
Why This Happens
Your Express/Django/FastAPI application returns Access-Control-Allow-Origin: https://app.com. Your Nginx config also adds add_header Access-Control-Allow-Origin https://app.com. Both headers are sent. Browsers reject multiple values for this header.
Option 1: Remove CORS headers from Nginx (recommended)
If your application already handles CORS correctly, remove the duplicate headers from Nginx:
# Delete or comment out: # add_header Access-Control-Allow-Origin $http_origin; # add_header Access-Control-Allow-Methods "GET, POST, OPTIONS"; # add_header Access-Control-Allow-Headers "Authorization, Content-Type";
Option 2: Remove CORS from your app, handle in Nginx
Centralise CORS handling at the Nginx layer if you have multiple services that need the same policy:
location / {
# Handle OPTIONS preflight
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin $http_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 Access-Control-Max-Age 3600 always;
return 204;
}
add_header Access-Control-Allow-Origin $http_origin always;
proxy_pass http://127.0.0.1:3000;
}
Then disable CORS handling in your application entirely — let Nginx own it.
Paste your nginx.conf to detect CORS header duplication and missing always flags.
Open Reverse Proxy Mapper →