I want to add a custom header for the response received from the server behind nginx.
While add_header
works for nginx-processed responses, it does nothing when the proxy_pass
is used.
I want to add a custom header for the response received from the server behind nginx.
While add_header
works for nginx-processed responses, it does nothing when the proxy_pass
is used.
add_header
works as well withproxy_pass
as without. I just today set up a configuration where I've used exactly that directive. I have to admit though that I've struggled as well setting this up without exactly recalling the reason, though.Right now I have a working configuration and it contains the following (among others):
Before nginx
1.7.5
add_header worked only on successful responses, in contrast to the HttpHeadersMoreModule mentioned by Sebastian Goodman in his answer.Since nginx
1.7.5
you can use the keywordalways
to include custom headers even in error responses. For example:Limitation: You cannot override the
server
header value usingadd_header
.As oliver writes:
However, as Shane writes, as of Nginx 1.7.5, you must pass
always
in order to getadd_header
to work for error responses, like so:There is a module called HttpHeadersMoreModule that gives you more control over headers. It does not come with Nginx and requires additional installation. With it, you can do something like this:
That will "set the Server output header to the custom value for any status code and any content type". It will replace headers that are already set or add them if unset.
You could try this solution :
In your
location
block when you useproxy_pass
do something like this:I'm not sure would it be exactly what you need but try some manipulation of this method and maybe result will fit your problem.
Also you can use this combination: