例如,我想这样做:
if ($http_user_agent ~ "MSIE 6.0" || $http_user_agent ~ "MSIE 7.0" (etc, etc)) {
rewrite ^ ${ROOT_ROOT}ancient/ last;
break;
}
而不是这个:
if ($http_user_agent ~ "MSIE 6.0") {
rewrite ^ ${ROOT_ROOT}ancient/ last;
break;
}
if ($http_user_agent ~ "MSIE 7.0") {
rewrite ^ ${ROOT_ROOT}ancient/ last;
break;
}
Nginx 拒绝这种语法(减去(等)),我在文档中没有看到任何关于此的内容。提前致谢。
此外,我们选择不使用 $ancient_browser 指令,所以这不是一个选项。
最佳答案
编辑:
由于阿列克谢十没有添加新答案,因此我将编辑我的答案以在这种情况下给出更好的答案。
if ($http_user_agent ~ "MSIE [67]\.")
原答案:Nginx 不允许多个或嵌套的 if 语句,但是您可以这样做:
set $test 0;
if ($http_user_agent ~ "MSIE 6\.0") {
set $test 1;
}
if ($http_user_agent ~ "MSIE 7\.0") {
set $test 1;
}
if ($test = 1) {
rewrite ^ ${ROOT_ROOT}ancient/ last;
}
它不是更短,但它允许您进行检查并将重写规则仅放置一次。替代答案:
在某些情况下,您还可以使用 | (管道)
if ($http_user_agent ~ "(MSIE 6\.0)|(MSIE 7\.0)") {
rewrite ^ ${ROOT_ROOT}ancient/ last;
}
关于nginx - 如何在 nginx "or"语句中使用 "if"运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29756330/