How to redirect to a new domain using nginx

In order to redirect to a different domain using nginx, use this snippet inside your server {...} block:

redirect_snippet.conf
location / {
    return 301 https://newdomain.com$request_uri;
}

Full example:

redirect_example.conf
server {
    server_name old.domain;
    listen 80;

    location / {
         return 301 https://techoverflow.net$request_uri;
    }
}

Check out similar posts by category: Nginx