简体   繁体   English

Nginx将外部域重定向到我自己的域

[英]Nginx redirect foreign domain to my own domain

There is one strange domain that is pointing to the IP address of my server. 有一个奇怪的域指向我的服务器的IP地址。 Sometimes DNS gets confused and it says that I am connected to that domain instead of my own. 有时DNS会感到困惑,并说我连接到该域而不是我的域。

I tried contacting the domain owner and domain registrar to remove the DNS A record that points to my machine but they weren't helpful at all 我尝试与域名所有者和域名注册商联系以删除指向我的计算机的DNS A记录,但它们根本没有帮助

Now I am trying to redirect: 现在,我尝试重定向:

www.foreigndomain.com www.foreigndomain.com

to

www.myowndomain.com www.myowndomain.com

so when someone types or opens www.foreigndomain.com it redirects to the my original domain instead serving my content under the www.foreigndomain.com. 因此,当有人键入或打开www.foreigndomain.com时,它将重定向到我的原始域,而不是在www.foreigndomain.com下提供我的内容。

I tried to add this to nginx.conf: 我试图将其添加到nginx.conf中:

server {
  server_name .foreigndomain.com;
  rewrite ^ http://www.myowndomain.com$request_uri? permanent;
}

but this creates a redirect loop, I'm not quite sure why. 但这会创建一个重定向循环,我不太清楚为什么。

How do I do this right? 我该怎么做对?

The redirect loop happens because www.myowndomain.com matches the same server that does the redirection, to fix this create another server to capture that server name 发生重定向循环是因为www.myowndomain.com与执行重定向的同一台服务器匹配,要解决此问题,请创建另一台服务器以捕获该服务器名称

server {
  server_name .foreigndomain.com;
  return 301 http://www.myowndomain.com$request_uri;
}
server {
  server_name www.myowndomain.com;
  location / {
    #config here
  }
}

If you already have a server with server name myowndomain.com then you need to add the www variant to it. 如果您已经拥有服务器名称为myowndomain.com的服务器,则需要向其添加www变体。

server {
  server_name myowndomain.com www.myowndomain.com;
  location / {
    # config here
  }
}

Try this rewrite variant: 尝试以下重写变体:

server {
  server_name .foreigndomain.com;
  return 301 http://www.myowndomain.com$request_uri;
}

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

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