2

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

jeffsp
  • 23
  • 4

1 Answers1

0

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; }
Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Hello, currently I ended up using the example I gave with multiple ifs but I'm having a little problem, for example: If I use: "if ($request_uri !~* "123")" or "if ($request_uri != "123")" it works well, but with variables its another history, with variables if I use "if ($request_uri != $test1)" it also works, but if I turn the "!=" different or equal, to different or contains "!~" "if ($request_uri !~ $test1)" it simply doesn't work, but its only using variables, any issue for this little problem? – jeffsp Jul 18 '20 at 16:38