详解Nginx如何配置继承模型

2019-10-17 17:27:14王旭

当然,它并不那么简单。 在位置内有三种可能的上下文,一个嵌套位置,一个if和limit_except。 指令的行为实际上完全取决于定义它的模块。 如果在该上下文中允许,则所有normal和array指令都将正确继承。 对于行动指令,故事有点不同。 通常它们不会继承到嵌套位置,但最终取决于模块的预期,并且它可以在指令的基础上有所不同。 这里没有使用nginx文档,所以你必须尝试一下,看看nginx是否会抱怨。 为了更好地衡量,让我们举一个最常见的行为示例以及它如何影响重写:

server {
  location /calendar {
    rewrite ^ /static.php; # Executes unless inner location matches.
 
    location ~ .php$ {
      fastcgi_pass backend; # Outer location context rewrite is not executed.  
    }
  }
}

try_files指令与上面提到的每个其他操作指令大致相同,不同之处在于,如果放置在服务器上下文中,nginx实际上会创建一个伪位置,该位置是可能的最不具体的位置。 这意味着如果请求与定义的位置匹配,则不会执行try_files指令。 这意味着如果您有location / defined,那么您有一个匹配每个可能请求的位置,因此try_files永远不会实际执行。 因此,如果可能的话,始终将try_files放在位置上下文而不是服务器上下文中

server {
  try_files $uri /index.php; # This never executes.
 
  location / {
    # Whatever here, or empty.
  }
 
  location ~ .php$ {
    # If this location executes then try_files still does not execute.
    # Even if location / did not exist.
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。