How does CORS interact with an API gateway or reverse proxy sitting in front of multiple backend microservices?
Short answer
When a browser calls through an API gateway acting as a single origin (routing to multiple backend services internally), CORS only needs to be configured once at the gateway layer since that's the origin the browser actually communicates with -- backend microservices behind the…
Advertisement
AModel answer
When a browser calls through an API gateway acting as a single origin (routing to multiple backend services internally), CORS only needs to be configured once at the gateway layer since that's the origin the browser actually communicates with -- backend microservices behind the gateway typically don't need their own CORS configuration since they're never called directly cross-origin by the browser, simplifying CORS management significantly in a microservices architecture.
// Browser only ever talks to the gateway origin:
// fetch('https://api.mycompany.com/orders') -- gateway handles CORS
// Gateway internally routes to http://orders-service:8080 (no CORS needed here,
// since this is server-to-server, not browser-to-server)Real-world example
A microservices platform configures CORS exactly once at their API gateway, eliminating the need to duplicate and maintain consistent CORS policies across dozens of individual backend services that the browser never contacts directly.
Likely follow-ups
- What CORS considerations remain if a gateway does server-side aggregation calling multiple backends?
- How does this simplification break down if some services are exposed directly, bypassing the gateway?
Related topics