nginx支持if语法,语法和平常的代码格式差不多:

 

if($xxx=xxx){
xxx
}

 

 

只是和代码不同的是,if条件语句判断相等只要一个等号,不是==

nginx虽然有if,但是却不支持else,如果想要构造else语法,可以使用下面的这个“小诀窍”:

 

 

server{
server_name*.maqian.io;
listen80;
    
location/{
set$is_matched0;
if($host=a.maqian.io){
proxy_pass https://127.0.0.1:1001/;
set$is_matched1;
}
        
if($host=b.maqian.io){
proxy_pass https://127.0.0.1:1002/;
set$is_matched1;
}
# 没有匹配到,跳转到默认页面
if($is_matched=0){
proxy_pass https://127.0.0.1;
}
        
# xxx
# xxx
# xxx
}
}