Realization of mathematical comparison function in Nginx if statement _nginx

Source: Internet
Author: User
Tags ereg

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 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.