简体   繁体   English

在 Nginx request_uri 中使用多个变量

[英]Using multiple variables in Nginx request_uri

Good morning programmers, I'm trying to protect a file, and for that I just want to allow the request_uri that I define.早上好,程序员们,我正在尝试保护一个文件,为此我只想允许我定义的 request_uri。 Example:例子:

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

In this case, that example works, but I would like to do something like this:在这种情况下,该示例有效,但我想做这样的事情:

set $test1 123;
set $test2 456;

if ($request_uri !~* "d=$test1&y=$test2") {
    return 403;
}

So I want to make it via variables, is that possible?所以我想通过变量来实现,这可能吗? Cause I already tested a bunch of examples and none of them worked.因为我已经测试了一堆示例,但没有一个有效。

PS: I'm using OpenResty (Nginx+Lua) so I would also accept solutions in Lua. PS:我使用的是OpenResty(Nginx+Lua)所以我也接受Lua中的解决方案。

As requested: I don't remember everything I tried, since I tried a bunch of code, but I can tell you something that did worked:根据要求:我不记得我尝试过的所有事情,因为我尝试了一堆代码,但我可以告诉你一些确实有效的东西:

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

The only problem on this sentence is that it only verifies for the d= and I wanted it to verify also the y= , I could do another if but I also wanted the d= and the y= on the same sentence instead of using multiple Ifs.这句话的唯一问题是它只验证d=我希望它也验证y= ,我可以做另一个if但我也想在同一个句子上使用d=y=而不是使用多个如果。 Anyway this sentence has another issue I can't change the $d != $teste to $d !~* $teste it simply stops working and I need to use the !~* , as last option I could use multipe ifs, but since I can't use the !~* it will not work anyway无论如何,这句话还有另一个问题,我无法将$d != $teste更改为$d !~* $teste 它只是停止工作,我需要使用!~*作为最后一个选项,我可以使用 multipe ifs,但是因为我不能使用!~*它无论如何都行不通

A regular expression is a literal and cannot contain variables.正则表达式是文字,不能包含变量。 Alternatively, you can achieve the same logic using:或者,您可以使用以下方法实现相同的逻辑:

set $test1 123;
set $test2 456;

if ($arg_d != $test1) { return 403; }
if ($arg_y != $test2) { return 403; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM