nginx add headers when returning 400 codes

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.

Leave a Comment