How to redirect to a different domain using NGINX? How to redirect to a different domain using NGINX? nginx nginx

How to redirect to a different domain using NGINX?


server_name supports suffix matches using .mydomain.com syntax:

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

or on any version 0.9.1 or higher:

server {  server_name .mydomain.com;  return 301 http://www.adifferentdomain.com$request_uri;}


server {    server_name .mydomain.com;    return 301 http://www.adifferentdomain.com$request_uri;}

http://wiki.nginx.org/HttpRewriteModule#return

and

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites


Why use the rewrite module if you can do return? Technically speaking, return is part of the rewrite module as you can read here but this snippet is easier to read imho.

server {    server_name  .domain.com;    return 302 $scheme://forwarded-domain.com;}

You can also give it a 301 redirect.