Did you know?
Access-Control-Allow-Origin can hold only one origin, so allowing several domains in Nginx needs a map.
Use map $http_origin to echo allowed origins; never list several in one header.
- Web servers
- Last checked
Explained
A common CORS mistake is listing several domains in one header, or adding Access-Control-Allow-Origin twice. Browsers reject both: the header must contain exactly one origin or *.
In Nginx, echo the request’s origin back only when it is on your list:
map $http_origin $cors_origin {
default "";
"https://app.example.com" $http_origin;
"https://admin.example.com" $http_origin;
}
# in the server or location block
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Vary Origin always;
Nginx skips headers with an empty value, so other origins get no allow header at all. Add Vary: Origin so caches don’t serve one site’s response to another. We tested this on nginx 1.28, 1.30 and 1.31.
Learn more
- How to Enable CORS in Nginx (Allow Origins, Preflight and Credentials) Full guide · Updated Dec 2026
