nginx.conf重定向多个条件 [英] nginx.conf redirect multiple conditions
本文介绍了nginx.conf重定向多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在两种情况下使用Nginx重定向请求.
I want to redirect requests on two conditions using nginx.
这不起作用:
if ($host = 'domain.com' || $host = 'domain2.com'){
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}
正确的方法是什么?
推荐答案
我之前也遇到过同样的问题.由于nginx不能执行复杂的条件或嵌套if语句,因此您需要评估两个以上的不同表达式.
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.
将变量设置为某个二进制值,然后在2个不同的if语句中,如果任一条件为true则启用:
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;
}
这篇关于nginx.conf重定向多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文