nginx:重定向到 if 块内的指定位置

Sel*_*vel 2 nginx

我在 nginx 中有一个位置,其中 GET 和 POST 请求应该发送到不同的命名位置。但这段代码不起作用:

if ( $request_method = GET ) {
try_files /NON-EXISTENT @named_location_get
}

if ( $request_method = POST ) {
try_files /NON-EXISTENT @named_location_post
}
Run Code Online (Sandbox Code Playgroud)

因为try_files不准进去if

还有其他方法可以将请求重定向到指定位置吗if

Ric*_*ith 5

我不知道在哪里记录了这一点,但我看到了在符号后面使用变量的配置@,以使命名条件成为location条件。

将其与map块一起使用,您可以消除if块。

我已经测试过这个例子:

map $request_method $name {
    GET      named_location_get;
    POST     named_location_post;
    default  named_location_other;
}
server {
    ...
    location ... {
        try_files nonexistent @$name;
    }
    location @named_location_get {
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)