遇到一个问题,在nginx中使用add_header时,只有当返回码是2xx,3xx时才生效,网上一搜果然,找到了一个很详细的解释,如下:
For nginx >= 1.7.5
Append "always" to the header definition:
add_header 'Access-Control-Allow-Origin' '*' always;
For nginx < 1.7.5
According to the nginx official document of ngx_header_module, the add_header can't work when response code is 400
syntax: add_header name value;
default: —
context: http, server, location, if in location
Adds the specified field to a response header provided that the response code equals
200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables.
In another way, you can try the HttpHeadersMoreModule, which is more powerful.
来源: https://stackoverflow.com/questions/20414669/nginx-add-headers-when-returning-400-codes