嵌套位置块中的指令继承 [英] Directive Inheritance in Nested Location Blocks

查看:89
本文介绍了嵌套位置块中的指令继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个nginx服务器块在语义上是否相同,还是有区别?第一个示例中特定于JSON的配置是否继承"/"位置的设置?是第二个例子吗?

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
  }
}

推荐答案

Nginx中config指令的继承使得指令只能从配置树上方的上下文中继承,而不能从同一级别或更低级别的上下文中继承.

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. 有些标准类型指令只有一个值或一组值 附加值.这些将简单地由较低的上下文继承 向下的配置树或在新的较低上下文中替换 价值观.一个例子是索引".

  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".

数组类型指令,它们在一个数组中传递多个单独的值 大批.这些将仅由下层的上下文继承 配置树或在该较低上下文中用新值替换. 请注意,您无法添加到阵列.更换零件正在更换 全部.一个示例是"proxy_param".因此,如果您定义proxy_param A, 例如,在服务器级别使用proxy_param B,然后尝试 在位置上下文中定义proxy_param C,"A"和"B"将是 清除(设置为默认值).定义"C"的意思是 替换数组.

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.

命令类型伪指令(例如"try_files")通常不是 完全继承了.

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.

这篇关于嵌套位置块中的指令继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆