我可以在 try_files nginx 指令中使用两个命名位置吗?

Posted

技术标签:

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

我想使用类似的东西:

location @a1 
    ...


location @a2 
    ...


location /path 
    try_files @a1 $uri @a2

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

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

【问题讨论】:

NGINX try_files with multiple named locations的可能重复 【参考方案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.

【讨论】:

以上是关于我可以在 try_files nginx 指令中使用两个命名位置吗?的主要内容,如果未能解决你的问题,请参考以下文章

使用nginx搭建RTMP服务器

Nginx 的 RTMP 模块的在线统计功能 stat 在 multi-worker 模式下存在 Bug

无法从 nginx-rtmp 获得破折号流式传输

nginx-rtmp-module

将 nginx rtmp 片段发送到 WebRTC

实现简单网页rtmp直播:nginx+ckplayer+linux