nginx.conf redirect multiple conditions

前端 未结 6 1821
傲寒
傲寒 2021-01-30 03:46

I want to redirect requests on two conditions using nginx.

This doesn\'t work:

  if ($host = \'domain.com\' || $host = \'domain2.com\'){
    rewrite ^/(         


        
6条回答
  •  长情又很酷
    2021-01-30 04:36

    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.com') {
      set $my_var 1;
    }
    if ($host = 'domain2.com') {
      set $my_var 1;
    }
    if ($my_var = 1) {
      rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
    }   
    

提交回复
热议问题