Score:0

nginx nested location directive inheritance when applies and when does not

cn flag

I know that I have to duplicate the proxy_pass in both locations below, do the proxy_set_headers also have to be duplicated?

location / {
  proxy_pass http://mywebfeservers;
  proxy_set_header  X-Real-IP  $remote_addr;
  proxy_set_header  X-Client-SSL  YES;
  proxy_set_header  X-Forwarded-For  $remote_addr;
  proxy_set_header  X-Forwarded-Host  $host;
  proxy_set_header  X-Forwarded-Port  443;
  proxy_set_header  X-Forwarded-Scheme  https;

  location /aaa {
    proxy_pass http://mywebfeservers;
    client_max_body_size 30M;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Client-SSL  YES;
    proxy_set_header  X-Forwarded-For  $remote_addr;
    proxy_set_header  X-Forwarded-Host  $host;
    proxy_set_header  X-Forwarded-Port  443;
    proxy_set_header  X-Forwarded-Scheme  https;
  }
}
Ivan Shatsky avatar
gr flag
Isn't it absolutely clear documented at the very beginning of the `proxy_set_header` directive [description](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header)? _These directives are inherited from the previous configuration level if and only if there are no `proxy_set_header` directives defined on the current level._
cn flag
@IvanShatsky Yes
Score:0
cg flag

Theoretically if you remove /aaa and leave / you should have the same effect.

If you need use the same configuration of proxy in diferente locations you can add the proxy config on a file.

Example:

  1. Create file proxy.conf and add this lines

Add

proxy_set_header  X-Real-IP  $remote_addr;
proxy_set_header  X-Client-SSL  YES;
proxy_set_header  X-Forwarded-For  $remote_addr;
proxy_set_header  X-Forwarded-Host  $host;
proxy_set_header  X-Forwarded-Port  443;
proxy_set_header  X-Forwarded-Scheme  https;
  1. Modify your configuration
    location / {
        proxy_pass http://mywebfeservers;
        include <your path>/proxy.conf
    }
    
    location /aaa {
       proxy_pass http://mywebfeservers;
       include <your path>/proxy.conf
    }
  1. Reload your configuration

The main objetive is create a modular configuration (multiple files) that allow you modify single file to modify multiples points of your configuration.

cn flag
I've slightly edited my question to add in why I wanted to have the nested location (the nested location adds in a client_max_body_size). As Ivan pointed out in the comment on the question the documentation, the proxy_set_header in my situation does not need to be duplicated, which avoids the need for the proxy.conf.
Score:0
cn flag

Slightly rephrased from the comment by Ivan Shatsky on the question.

It is absolutely clearly documented at the very beginning of the proxy_set_header directive description.

These directives are inherited from the previous configuration level if and only if there are no proxy_set_header directives defined on the current level.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.