[Nginx] 4xx and 5xx retain custom Headers
Problem
If the status code returned by the request is 404 or 50x, the custom header is still returned.
Analysis and Solution
The add_header section in the nginx document contains the following sentence:
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.
That is to say, add_header generally does not apply to 4xx and 5xx responses.
However, you can use the always keyword after version 1.7.5. Let's test it.
Configuration snippets
location /hello { add_header Access-Control-Allow-Origin * always; return 404; }
Test Results
curl -i http://127.0.0.1:9999/helloHTTP/1.1 404 Not FoundServer: openresty/1.9.3.1Date: Mon, 01 Feb 2016 11:17:59 GMTContent-Type: text/htmlContent-Length: 174Connection: keep-aliveAccess-Control-Allow-Origin: *
The test is successful.
Expansion module
You can also use headers-more-nginx-module written by chunge to do things. For more information, see the documentation.
REF
http://serverfault.com/questions/418709/nginx-add-header-for-a-50-page