Using multiple variables in Nginx request_uri

Viewed 126

Good morning programmers, I'm trying to protect a file, and for that I just want to allow the request_uri that I define. 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.

P.S: I'm using OpenResty (Nginx+Lua) so I would also accept solutions in 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. 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

1 Answers

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; }
Related