Access-Control-Allow-Origin with multiple origin domains
Codemia Knowledge Hub ·
The Access-Control-Allow-Origin (ACAO) header cannot list several domains. To allow more than one origin, the server reads the request's Origin header, checks it against a list of allowed origins, and returns that single origin in ACAO when it matches.
How CORS and ACAO work
CORS is enforced by browsers to stop malicious sites from reading resources and data from other sites without permission. By default, browsers restrict cross-origin HTTP requests made from scripts. A JavaScript app on https://domain-a.com cannot read resources from https://domain-b.com unless domain-b.com permits it with CORS headers such as Access-Control-Allow-Origin.
ACAO says which origin may access the resource. Its value can be:
- A specific origin, such as
https://example.com. *, allowing any origin (not allowed for requests with credentials).null, which matches the opaquenullorigin (for example sandboxed iframes orfile:pages). It does not block everyone, and it should not be used as a "deny" value.
Why a comma-separated list fails
Setting Access-Control-Allow-Origin: https://domain-a.com, https://domain-b.com is not valid. The header holds one origin (or *), so multiple origins need conditional handling.
Echoing the allowed origin from the server
The most flexible approach is to set ACAO dynamically from the incoming request's origin, responding only when that origin is in an allowlist.
Suppose only https://domain-a.com and https://domain-b.com may access https://api.server.com:
For origins that are not allowed, omitting the header is the safer choice than returning "null".
Handling it in a reverse proxy
A reverse proxy can also add or modify CORS headers, using rules more complex than you may want to write in application code.
Security rules for multiple origins
- Always validate the
Originheader against an allowlist. - Do not use
Access-Control-Allow-Origin: *with credentials (cookies, HTTP authentication, client-side SSL certificates). - Consider how CORS fits your overall security posture and use measures such as HTTPS.
| Feature | Details |
| ACAO Single Origin | Set by Access-Control-Allow-Origin: https://example.com |
| ACAO Multiple Origins | Not directly supported, requires conditional handling |
| ACAO Wildcard | Set by Access-Control-Allow-Origin: * (use cautiously) |
| Server Implementation | Dynamic ACAO response based on a whitelist of origins |
| Security | Validate every request against a list of allowed origins |
Related reading
- Access denied for user 'rootlocalhost' using passwordNO
- Access denied; you need at least one of the SUPER privileges for this DEFINER operation
- ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN
- Access to Modified Closure 2
- AccessDenied Not authorized to perform stsAssumeRoleWithWebIdentity
- AccessDeniedException User is not authorized to perform lambdaInvokeFunction
- Accessing Kafka broker from outside my LAN
- Accessing RDS from within a Docker container not getting through security group?
System design interview problems on this topic
- Design a Secure Identity Management System: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
- Design a Network Security Monitoring Tool: solution and walkthrough
- Design a CAPTCHA System: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.