在 Nginx 中使用变量 request_uri

Using variables in Nginx request_uri

程序员们早上好, 我正在尝试保护一个文件,为此我只想允许我定义的 request_uri。 示例:

if ($request_uri !~* "d=123") {
return 403;
}

在这种情况下,该示例有效,但我想做这样的事情:

set $teste 123;
if ($request_uri !~* "d=$teste") {
return 403;
}

所以我想通过变量来实现,可以吗?因为我已经测试了一堆示例并且 none 其中有效。

P.S: 我正在使用 OpenResty (Nginx+Lua) 所以如果 lua 有可能的解决方案我也会接受它。

假设 d 是你的查询参数,123 是它的值,你可以尝试这样的事情:

set $teste 123;
if ($request_uri ~* "[?&]d=([^&]*)") { set $d ; }
if ($d != $teste) {
    return 403;
}