Nginx If support =,!= logical comparison, but does not support if <, <, >=, <= comparison.
This example uses the Set-misc-nginx-module
Copy Code code as follows:
Location =/test/{
Default_type html;
Set_random $a 0 9; # $a randomly fetched from 0-9
if ($a <= 4) {# $a if < 4 that's the wrong notation.
Echo ' A: $a is LTE 4 ';
}
if ($a >= 5) {# $a if > 5 that's the wrong notation.
Echo ' A: $a is GTE 5 ';
}
}
the above configuration, when starting Nginx will be reported incorrectly.
That is not supported, there is no way to make up for the small?
Copy Code code as follows:
Location =/test/{
Default_type html;
Set_random $a 0 9; # $a randomly fetched from 0-9
if ($a ~ [0-4]) {# $a if the regular match 0-4
Echo ' A: $a is LTE 4 ';
}
if ($a ~ [5-9]) {# $a if the regular match 5-9
Echo ' A: $a is GTE 5 ';
}
}
Test 10 times:
Copy Code code as follows:
A:8 is GTE 5
A:9 is GTE 5
A:2 is LTE 4
A:1 is LTE 4
A:8 is GTE 5
A:0 is LTE 4
A:9 is GTE 5
A:1 is LTE 4
A:4 is LTE 4
A:5 is GTE 5
...
: Nginx unexpectedly does not support this kind of writing ...
Copy Code code as follows:
Location =/test/{
Default_type html;
Set_random $a 0 9; # $a randomly fetched from 0-9
Set_random $b 0 9; # $b randomly fetched from 0-9
Set $ereg "[0-$b]";
if ($a ~ $ereg) {# $a if the regular match 0-$b
Echo ' A: $a is LTE B: $b ereg: $ereg ';
}
if ($a!~ $ereg) {# $a if the regular does not match 0-$b
Echo ' A: $a is GT B: $b ereg: $ereg ';
}
}
beg Daniel to realize ...