Even though this was asked long ago, I was compiling nginx with more module, but with newer version of nginx, I found I don't have to custom compile nginx, all I needed was to add always
directive.
http://nginx.org/en/docs/http/ngx_http_headers_module.html
Syntax: add_header name value [always];
If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.
So a tuned version of CORS headers:
if ($cors = "trueget") {
# Tells the browser this origin may make cross-origin requests
# (Here, we echo the requesting origin, which matched the whitelist.)
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
# Tells the browser it may show the response, when XmlHttpRequest.withCredentials=true.
add_header 'Access-Control-Allow-Credentials' 'true' always;
}