5

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?

2 Answers 2

7

Answer found here:

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:

http://wiki.nginx.org/NginxHttpHeadersMoreModule

6

As of v1.7.5 of nginx, you can add "always" to the end of the add_header line to solve this.

2
  • Are you sure? Is there documentation that states this? I am using nginx/1.8.0 and I don't get the CORS headers returned on 401 or 403 (which is what I want).
    – JMac
    Commented Aug 31, 2016 at 3:56
  • Answering way too late. From nginx.org/en/docs/http/ngx_http_headers_module.html "If the always parameter is specified (1.7.5), the header field will be added regardless of the response code."
    – Thabo
    Commented Mar 22, 2021 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.