【问题标题】:double try_files directive for the same location in nginxnginx 中同一位置的双重 try_files 指令
【发布时间】:2016-11-29 15:35:30
【问题描述】:

我在一些要点和配置示例中发现了一个 nginx 配置 sn-p(主要用于 PHP 应用程序):

#site root is redirected to the app boot script
location = / {
    try_files @site @site;
}
#all other locations try other files first and go to our front controller if none of them exists
location / {
    try_files $uri $uri/ @site;
}

但我就是不明白:第一个 try_files 指令是否匹配?对我来说,这看起来像是一些无意义的黑客行为。

请确认或解释为什么不 - 谢谢:)

【问题讨论】:

  • 无条件跳转到@site 是一种技巧。就个人而言,我更喜欢error_page 变体。见this answer。

标签: nginx


【解决方案1】:

这就是这里发生的事情:

第一个位置= /仅在请求中的路径为/时使用,例如http://example.com/。第二个位置 / 用于所有其他 URL,例如http://example.com/foo 或 http://example.com/bar。

第一个位置的原因是为了避免来自index-directives 的任何干扰,这些指令重定向到 index.html 或类似的东西。

在第一个位置内,try_files-指令首先查找名为 @site 的文件,该文件不存在,然后重定向到命名位置 @site。这种构造的原因是到指定位置的重定向纯粹是内部的,即@site 位置不能直接从客户端访问,并且$uri 在此重定向期间保持不变(对于其他重定向)。第一个参数@site 可以是除真实现有文件之外的任何内容。为了清楚起见,我更喜欢称它为DUMMY。

第二个位置首先尝试静态文件,如果找不到,也会重定向到指定位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2012-11-09
    • 2020-03-26
    相关资源
    最近更新 更多