【问题标题】:May I use two named locations within try_files nginx directive?我可以在 try_files nginx 指令中使用两个命名位置吗?
【发布时间】:2016-10-24 02:05:08
【问题描述】:

我想使用类似的东西:

location @a1 {
    ...
}

location @a2 {
    ...
}

location /path {
    try_files @a1 $uri @a2
}

我应该在@a1 位置继续尝试 $uri / @a2 吗?

如果是这样,nginx 将如何处理?

【问题讨论】:

标签: nginx location


【解决方案1】:

我找到了this solution:

location /static/ {
    try_files $uri @static_svr1;
}
location @static_svr1{
    proxy_pass http://192.168.1.11$uri;
    proxy_intercept_errors on;
    recursive_error_pages on;
    error_page 404 = @static_svr2;
}

location @static_svr2{
    proxy_pass http://192.168.1.12$uri;
    proxy_intercept_errors on;
    recursive_error_pages on;
    error_page 404 = @static_svr3;
}

location @static_svr3{
    proxy_pass http://192.168.1.13$uri;
}

这个例子的工作原理是:

try_files $uri @static_svr1 @static_svr2 @static_svr3

【讨论】:

    【解决方案2】:

    您不能,因为 try_files 只会将其 last 参数视为命名位置 - 在您的示例中,nginx 将查找名为 @a1 的文件并忽略 location @a1 块。

    try_files docs 对此并不十分明确,只是提到“最后一个参数也可以指向一个命名位置”。

    This answer 显示了一个使用error_page 的解决方法,这可能对您有用(取决于您想通过进入@a1 执行什么操作)。

    More on try_files here.

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 2018-12-19
      • 2012-04-22
      • 1970-01-01
      • 2014-10-08
      相关资源
      最近更新 更多