nginx - 如何在 nginx "or"语句中使用 "if"运算符?

标签 nginx

例如,我想这样做:

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/

相关文章:

nginx - 我可以使用 Nginx 重定向来自 SSL 端口的非 SSL 流量吗

ruby-on-rails-4 - 带有运行 Puma 和 Nginx 的 AWS Elastic Beanstalk 的 Rails 应用程序 502

ruby - 如何在 Elastic Beanstalk 中隐藏 Nginx 版本

amazon-web-services - AWS Elastic Beanstalk、Dockerrun.aws.json 和 docker run 上的多个端口

ruby-on-rails - unicorn 和 nginx 的奇怪问题导致 502 错误

php - 在 Vagrant 中使用 XDEBUG

nginx - NodeMediaServer MP4 更改默认视频分辨率 540p

tomcat - nginx -> Tomcat 与文件夹路径 - 重定向太多

nginx - Nginx多个服务器 block 监听相同的端口

python - 当我进行重定向时,Django 会加倍我的获取参数,但仅在我们的服务器上