Permanent redirects with Nginx

Recently, I had the need to redirect all traffic from one domain to identical URLs on a new domain.  Luckily, NGINX provides a simple way to accomplish this.

The following will work as an available site for domain www.domainone.com on any NGINX version after 0.9.1:

server {
server_name www.domainone.com;
return 301 http://www.domaintwo.com$request_uri;
}

The server will listen on port 80 for request to www.domainone.com and redirect to the same request at www.domaintwo.com.

Note that the server is being asked to provide a 301 status code to inform the requesting client that the redirect is permanent. If you are wishing to redirect encrypted traffic then you will need a similar block listening at port 443.