Nginx services fails for cross-domain requests if the service returns error
Solution 1:
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/
Solution 2:
You can use always directive starting nginx 1.7.5 (* Feature: the "always" parameter of the "add_header" directive.)
add_header 'Access-Control-Allow-Origin' '*' always;