Nginx services fails for cross-domain requests if the service returns error

cross-domainnginx

I am making a cross domain request in my web app.

I have set the CORS headers on Nginx. Everything is working fine except when the service returns an error like 404, 400, 500 etc, instead of receiving the error code, the service is failing with an error saying that the Origin *********** is not allowed by Access-Control-Allow-Origin.

Any ideas why this might be happening?

This is what the nginx setting looks like;

location /api {
    add_header Access-Control-Allow-Origin *;
}

Best Answer

Unfortunately add_header won't work with status codes other than 200, 204, 301, 302 or 304. You can find this in the documentation here.

You may be able to use this plugin to do what you want:

https://www.nginx.com/resources/wiki/modules/headers_more/