If would like to configure redirecting a domain1.com to domain2.com in NGINX server, there are two major good rewrite rules here,


rewrite ^ http://example.com$request_uri? permanent;

return 301 http://example.com$request_uri;


server_name supports suffix matches using .mydomain.com, just create a virtualhost and add the below lines,

server {
  server_name domain1.com;
  return 301 https://www.domain2.com$request_uri;
}

or

server {
  server_name domain1.com;
rewrite ^ https://www.domain2.com$request_uri? permanent;
}