nginx.conf redirect multiple conditions

I had this same problem before. Because Nginx can’t do complex conditions or nested if statements, you need to evaluate over 2 different expressions.

set a variable to some binary value then enable if either condition is true in 2 different if statements:

set $my_var 0;
if ($host="domain.example") {
  set $my_var 1;
}
if ($host="domain2.example") {
  set $my_var 1;
}
if ($my_var = 1) {
  rewrite ^/(.*)$ http://www.domain.example/$1 permanent;
}

Leave a Comment