Nginx中IF语句实现数学比较功能
nginx的if支持=、!=逻辑比较,但不支持if中<、<、>=、<=比较.
本示例使用了set-misc-nginx-module
location=/test/{
default_typehtml;
set_random$a09; #$a随机从0-9取
if($a<=4){ #$a如果<4这是错误的写法
echo'a:$aislte4';
}
if($a>=5){ #$a如果>5这是错误的写法
echo'a:$aisgte5';
}
}
上面的配置,在启动nginx时会报错误的.
即然不支持,那有没有办法小小地弥补下呢?
location=/test/{
default_typehtml;
set_random$a09; #$a随机从0-9取
if($a~[0-4]){ #$a如果正则匹配0-4
echo'a:$aislte4';
}
if($a~[5-9]){ #$a如果正则匹配5-9
echo'a:$aisgte5';
}
}
测试10次:
a:8isgte5 a:9isgte5 a:2islte4 a:1islte4 a:8isgte5 a:0islte4 a:9isgte5 a:1islte4 a:4islte4 a:5isgte5 ...
附:NGINX竟然不支持这样的写法....
location=/test/{
default_typehtml;
set_random$a09; #$a随机从0-9取
set_random$b09; #$b随机从0-9取
set$ereg"[0-$b]";
if($a~$ereg){ #$a如果正则匹配0-$b
echo'a:$aislteb:$b ereg:$ereg';
}
if($a!~$ereg){ #$a如果正则不匹配0-$b
echo'a:$aisgtb:$b ereg:$ereg';
}
}
求大牛来实现...