NGINX is a popular web server used by many high traffic websites and online stores. Often web developers and system administrators need to move a site or a URL to an entirely new domain. In such cases, they need to redirect location to another domain in NGINX. In this article, we will learn how to do this.
How to Redirect Location to Another Domain in NGINX
There are two ways to redirect location in NGINX – permanent and temporary. Permanent redirection tells web browsers and search engines that the location has permanently moved so its new location can be cached for later use. In this case, NGINX will return response code 301. Temporary redirection means the location has only temporarily been moved and might come back. So web browsers and search engines must not save the redirection. In this case, NGINX will return response code 302.
For both cases, you need to use return directive to return the appropriate response code as well as redirect user to new location. The return directive will immediately stop processing request and send redirect command back to client web browser. Based on the response received from server, the browser can immediately redirect.
Permanent Redirection
If you have permanently moved the URL to a new location then it is best to set up permanent redirection (HTTP response 301). This will help search engines to note the change and pass on the SEO juice from old location to new location. Also, client web browsers will cache this change and directly take users to new location whenever the old location is requested on their browsers.
Open NGINX configuration file in text editor.
sudo vi /etc/nginx/nginx.conf
OR
sudo vi /etc/nginx/sites-available/nginx.conf
Let us say you want to redirect old-site.com/old-url/ to new domain new-site.com, then add the following location.
server {
listen 80;
server_name old-site.com www.old-site.com;
location /old-url/ {
return 301 new-site.com;
}
}
Save and close the file. In the above code, we use return statement to permanently redirect URL. When NGINX receives request for old URL, it will match this location and execute the return statement. This will return response code 301 to web browser, along with new URL location and then the browser will go to the new location.
The above config will redirect all URLs starting with /old-url/ along with the request path and query parameters. For example, old-site.com/old-url/product.html will be redirected to new-site.com/old-url/product.html
If you want to redirect to new-site.com/old-url/ then update the return statement accordingly.
location /old-url/ {
return 301 new-site.com/old-url/;
}
Lastly, restart NGINX server to apply changes.
sudo systemctl reload nginx
OR
sudo systemctl restart nginx
Temporary Redirection
On the other hand, if you have temporarily moved the URL to new domain but do not want search engines to permanently store the redirection and transfer SEO juice of old URL to new domain, then use a temporary redirection. In this case, NGINX will return response code of 302 indicating temporary redirection.
Open NGINX config file.
sudo vi /etc/nginx/nginx.conf
OR
sudo vi /etc/nginx/sites-available/nginx.conf
Add the following location block to it. It is similar to the previous code, except instead of 301 we are returning 302 response code.
server {
listen 80;
server_name old-site.com www.old-site.com;
location /old-url/ {
return 302 new-site.com;
}
}
Save and close the file. In the above code, the location block will match all requests starting with /old-url/ and redirect it to new location along with response code 302.
Lastly, restart or reload NGINX server to apply changes.
sudo systemctl reload nginx
OR
sudo systemctl restart nginx
Conclusion
In this article, we have learnt how to redirect location to another domain in NGINX server. We have learnt how to do this permanently as well as temporarily. You can use either of these solutions, depending on your requirement.
Also read:
How to Configure Multiple Host Names in NGINX
How to Increase File Upload Size in NGINX
How to Block Image Hotlinking But Allow Google

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.