简体   繁体   中英

Nginx redirect foreign domain to my own domain

There is one strange domain that is pointing to the IP address of my server. Sometimes DNS gets confused and it says that I am connected to that domain instead of my own.

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

Now I am trying to redirect:

www.foreigndomain.com

to

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.

I tried to add this to 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

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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