How can I use the "or" operator in the nginx "if" statement?

For example, I want to do this:

if ($http_user_agent ~ "MSIE 6.0" || $http_user_agent ~ "MSIE 7.0" (etc, etc)) {
    rewrite ^ ${ROOT_ROOT}ancient/ last;
   break;
}

      

instead of this:

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 rejects this syntax (minus (and so on)) and I don't see anything in the docs about it. Thanks in advance.

Also, we decided not to use the $ antique_browser directive, so not an option.

+3


source to share


1 answer


Edit:

Since Alexey Ten hasn't added a new answer, I'll edit mine to give him a better answer in this case.

if ($http_user_agent ~ "MSIE [67]\.")

      

Original answer:

Nginx does not allow multiple or nested if statements, but you can do this:



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;
}   

      

It is not shorter, but it allows you to validate and place the rewrite rule only once.

Alternative answer:

In some cases, you can also use | (Trumpet)

if ($http_user_agent ~ "(MSIE 6.0)|(MSIE 7.0)") {
  rewrite ^ ${ROOT_ROOT}ancient/ last;
}  

      

+10


source







All Articles