Nginx中进行简单的if-else判断
location /aaa/ {
proxy_pass http://localhost:8080/aaa/;
# 因为Nginx中只支持简单的if语句,所以用以下方式模拟and条件
set $flag "";
if ($query_string ~ ".*(?:^|\?|&)redirect=healthClaim(?:(?:&.*)|$)") {
set $flag "${flag}1";
}
if ($query_string ~ ".*(?:^|\?|&)redirect=healthClaim(?:(?:&.*)|$)") {
set $flag "${flag}1";
}
if ($flag ~* "11") {
# 这里写redirect不为url1和url2的代码
rewrite ^/aaa/user/index(.*) https://test.com.cn/bbb/index.html last; # 主页
rewrite ^/aaa/user/login(.*) https://test.com.cn/bbb/login.html last; # 登陆
}
}
本文介绍如何在Nginx中使用简单的if语句来实现条件判断,通过设置变量和正则表达式匹配来实现特定URL的重定向,适用于需要根据不同查询参数执行不同操作的场景。
1545

被折叠的 条评论
为什么被折叠?



