if的逻辑用法
什么是逻辑用法呢, 就程序中的and、or关系, 就叫做逻辑了.
NGINX支持if的 and 与 or 或者 && 与 || 吗?
答案是No. 当你尝试这样配置, 重载nginx时, nginx会报出错误
1location = /test/ { 2 default_type text/html; 3 set $b 0; 4 if ( $remote_addr != '' && $http_x_forwarded_for != '' ){ 5 set $b '1'; 6 } 7 echo $b; 8}
[root@test-vm ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] invalid condition "$remote_addr" in /usr/local/nginx/conf/nginx.conf:60 configuration file /usr/local/nginx/conf/nginx.conf test failed 那么我们应该怎样来实现and 和or的逻辑关系呢?
1location = /test_and/ { 2 default_type text/html; 3 set $a 0; 4 set $b 0; 5 if ( $remote_addr != '' ){ 6 set $a 1; 7 } 8 if ( $http_x_forwarded_for != '' ){ 9 set $a 1$a; 10 } 11 if ( $a = 11 ){ 12 set $b 1; 13 } 14 echo $b; 15} 16location = /test_or/ { 17 default_type text/html; 18 set $a 0; 19 set $b 0; 20 if ( $remote_addr != '' ){ 21 set $a 1; 22 } 23 if ( $http_x_forwarded_for != '' ){ 24 set $a 1; 25 } 26 if ( $a = 1 ){ 27 set $b 1; 28 } 29 echo $b; 30}
