Nginx – Inherit proxy_set_header when using it in location block

nginx

I am trying to set up a bunch of virtual hosts on Nginx and I want to set some headers before passing the request upstream. I have some headers that I always want to set and then I have some headers that I want to set for selected location blocks.

Unfortunately, it seems that Nginx does not allow the proxy_set_header directive on multiple levels and as a result, it just clears any headers set in the http block.

Nginx documentation states the following on proxy_set_header:

Allows redefining or appending fields to the request header passed to
the proxied server. The value can contain text, variables, and their
combinations. These directives are inherited from the previous level
if and only if there are no proxy_set_header directives defined on the
current level
.

So the question is… either Nginx has a disappointing limitation or I am thinking about this problem in the wrong way.

Any ideas on an idiomatic solution to this problem?

Best Answer

Create a file to include in each block that sets the headers you want. (So a common inc-http-headers.conf) that you include in the http block and all of the location blocks that need the common headers.