38

are the following two nginx server blocks semantically the same, or is there any difference? Does the JSON-specific configuration in the first example inherit the settings of the "/" location? Does it in the second example?

server {
  location / {
     # ...
     location ~* \.json$ {
          # json-specific settings
     }
  }
}

server {
  location / {
     # ...
  }
  location ~*  \.json$ {
    # json-specific settings
  }
}
1

1 Answer 1

41

The inheritance of config directives in Nginx is such that directives can only be inherited from contexts higher up the configuration tree and never from contexts on the same level or lower.

So, a location block cannot inherit from another location block but a nested location block can inherit from the parent location block.

I stressed can because there are a number of different types of directives and the inheritance behaviour is a bit different for each.

  1. There are Standard Type Directives which only have one value or set of values attached. These will simply be inherited by contexts lower down the config tree or replaced within that lower context by new values. An example is "index".

  2. Array Type Directives which pass multiple separate values in an array. These will simply be inherited by contexts lower down the config tree or replaced within that lower context by new values. Note that you cannot add to the array. Changing part is replacing it all. An example is "proxy_param". So if you define proxy_param A and proxy_param B at the server level for instance, and then try to define proxy_param C in a location context, "A" and "B" would be wiped out (set to default values). as defining "C" has meant replacing the array.

  3. Command Type Directives such as "try_files" are generally not inherited at all.

So specifically to your question, directives defined in one location block context cannot be inherited by another as in your second example.

Standard and Array type directives defined in the parent location block will be inherited by the nested location block. Command type directives defined in the parent will not be inherited in general.

8
  • 63
    proxy_pass also isn't inherited by a nested location.
    – saaj
    Dec 9, 2015 at 14:23
  • 3
    uwsgi_pass also isn't inherited.
    – Rockallite
    Mar 25, 2016 at 4:41
  • 46
    Is there any damned reference table one can consult to know which directive is or isn't inherited ? What a pain having to come to SO then click on a link in a comment, pointing to another forum, reading who-knows stating that "proxy_pass isn't inherited" Dec 11, 2017 at 9:35
  • 13
    Seems like a pretty serious flaw that e.g. add_header in child context can't include headers from the parent context. That's a serious gotcha, and one that is very non-intuitive, and will lead to duplicated lines of config.
    – Josh M.
    Jul 9, 2020 at 19:38
  • 15
    With so many odd quirks and flaws, I struggle to understand why nginx is so popular. I'm starting to think I should have gone with ATS or something else for my reverse proxy.
    – TyR
    Sep 6, 2020 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.