Nginx中IF、AND、OR語句用法實例
在上一篇文章:《Nginx if語句加正則表達式實現(xiàn)字符串截斷》中, 我們介紹過了使用if來進行截斷字符串的用法, 這次我們來了解下if的邏輯用法:
什么是邏輯用法呢, 就程序中的and、or關系, 就叫做邏輯了.
NGINX支持if的 and 與 or 或者 && 與 || 嗎?
答案是No.
當你嘗試這樣配置, 重載nginx時, nginx會報出錯誤
location = /test/ {
default_type text/html;
set $b 0;
if ( $remote_addr != '' && $http_x_forwarded_for != '' ){
set $b '1';
}
echo $b;
}
[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
那么我們應該怎樣來實現(xiàn)and 和or的邏輯關系呢?
location = /test_and/ {
default_type text/html;
set $a 0;
set $b 0;
if ( $remote_addr != '' ){
set $a 1;
}
if ( $http_x_forwarded_for != '' ){
set $a 1$a;
}
if ( $a = 11 ){
set $b 1;
}
echo $b;
}
location = /test_or/ {
default_type text/html;
set $a 0;
set $b 0;
if ( $remote_addr != '' ){
set $a 1;
}
if ( $http_x_forwarded_for != '' ){
set $a 1;
}
if ( $a = 1 ){
set $b 1;
}
echo $b;
}
相關文章
nginx?ingress代理websocket流量的配置方法
ingress?nginx默認支持websocket協(xié)議,使用長連接協(xié)議時需要注意連接超時的設置,文中有提到讀取和發(fā)送超時的注解參數(shù),通過本文閱讀可以快速掌握,對nginx?ingress代理websocket相關知識感興趣的朋友一起看看吧2022-03-03詳解nginx中l(wèi)ocation、rewrite用法總結
這篇文章主要介紹了詳解nginx中l(wèi)ocation、rewrite用法總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09Nginx配置動態(tài)代理后通過curl訪問報403問題
本文主要介紹了Nginx配置動態(tài)代理后通過curl訪問報403問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06