Context
I use Docker to deploy my Rails application with an nginx container as my front HTTP server.
I configured rack-cors in Rails to send CORS headers for some URLs.
My application is available using 2 different domains using HTTPS, configured in nginx as 2 different server
configurations. This first one configured as the default_server
.
Internal communications between nginx and Rails are using HTTP. HTTPS is only configured in nginx.
To be clear, what I mean by CORS headers are:
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Expose-Headers
Access-Control-Max-Age
I am using rack-cors 1.0.2 and nginx 1.16.0.
I created a Gist with my nginx configuration.
Problem
When using the 2nd domain using HTTPS, my CORS headers set by rack-cors are removed by nginx.
I know they are removed because I monitored multiple things:
- I put rack-cors in debug mode and it correctly outputs the CORS headers in Rails logs.
- I performed the query directly to Rails from within the nginx container and the response contained the CORS headers.
- I monitored the query/response made by nginx to Rails using tcpdump and the response contained the CORS headers.
But the response made by nginx to the end user does not contain the CORS headers.
All this time, responses sent for the first domain are OK and contain the CORS headers.
Questions
- Why is nginx removing those headers from the upstream response?
- How can I monitor this behavior in nginx?
- How can I prevent nginx from rewriting these response?
Thanks