简体   繁体   English

nginx重定向到特定域

[英]nginx redirect for a specific domain

I would like to redirect traffic from: 我想从重定向流量:

     http://demo.foo.com/a.png
to   http://demo.bar.com/a.png

One caveat is that foo.com and bar.com are both the same box. 需要注意的是foo.combar.com都是同一个盒子。 So I need specify that the redirect occur for foo.com traffic only, to avoid a redirect loop on bar.com traffic. 因此,我需要指定仅对foo.com流量进行重定向,以避免bar.com流量发生重定向循环。

I tried this: 我尝试了这个:

server {
  server_name  demo.foo.com;
  return       301 $scheme://demo.bar.com$request_uri;
}

After waiting a minute for it to take effect... This looked promising: 等待一分钟使其生效...这看起来很有希望:

curl -I http://demo.foo.com

The 301 appeared as expected. 301出现预期。 However, there are two issues: 但是,有两个问题:

1) running curl -I http://demo.bar.com also came back with a 301 redirect to demo.bar.com (so my server_name filter is not working) 1)运行curl -I http://demo.bar.com也返回了301重定向到demo.bar.com (因此我的server_name过滤器不起作用)
2) after a few more minutes (~2-3) both curl commands stop working, and return a 503 Unavailable message (this may be due to a redirect loop caused by #1) 2)再过几分钟(〜2-3)后,两个curl命令都停止工作,并返回503不可用消息(这可能是由于#1导致的重定向循环)

We are using such constraction: 我们正在使用这样的约束:

server {
   ...

   server_name demo.foo.com demo.bar.com;

   if ($http_host = "demo.foo.com") {
      rewrite ^ http://demo.bar.com:$server_port$request_uri permanent;
   }

   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM