我想使用 Nginx 在两种情况下重定向请求。
这不起作用:
if ($host = 'domain.example' || $host = 'domain2.example'){
rewrite ^/(.*)$ http://www.domain.example/$1 permanent;
}
正确的做法是什么?
最佳答案
我以前也遇到过同样的问题。由于 Nginx 无法执行复杂的条件或嵌套的 if 语句,因此您需要计算 2 个以上不同的表达式。
将变量设置为某个二进制值,然后在两个不同的 if 语句中任一条件为 true 时启用:
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;
}
关于redirect - nginx.conf 重定向多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833238/