1

i have a server droplet in Digital ocean , two domains pointing out to that server A.com and B.com . i want that when A.Com is call then nginx redirect it to B.com and server as B.com . e.g if user hit url from browser B.com then my site should be shown but if it Hit A.com then same site should be open but domain changes to B.com

Hassan Amjad
  • 399
  • 1
  • 6
  • 20

2 Answers2

5

This is some example how to do it:

server {
    listen 443 ssl http2;
    server_name  a.com;
    location / {
      return 301 $scheme://b.com$request_uri;
    }
}
IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • why can't we use the proxy_pass instead of return? can you please help me understand that? – Kraken Nov 24 '22 at 19:55
  • 3
    Because `proxy_pass` will use more resources on the server because the data will come through nginX twice. With `return` the connection is closed and the browser is adviced to open a new connection to the new domain. – IVO GELOV Nov 25 '22 at 11:29
2

Add this server block

This is to redirect

server {
   listen 80;
   server_name example.com; // your domain name
   location / {
          return 301 https://redirect-to.com; //Add your redirect url here
   }
}
Anas M.I
  • 512
  • 2
  • 8