How to Create Nginx Redirect (Temporary and Permanent)

Nginx (pronounced engine-x) is a powerful open source high performing HTTP web server. It can work as a reverse proxy or POP3/IMAP proxy. It is the third most popular web server and well known for its enhanced performance, ease of use and configuration, stability and minimum resource utilization. That’s why in this tutorial, we’ll show you how to use Nginx to redirect traffic in different ways.

According to Datanyze, Nginx 32% market share in the web server market. It supports a lot of renowned websites such as Github, Netflix, Cloudflare, Hulu,  Airbnb, Hulu, WordPress, and many more.

Redirection in Nginx

The ability to forward the URL of the website to another address or point based on your criteria is an essential feature of the Nginx web server. An Nginx redirect is simple and easy to set up. Often users choose to redirect a page that has good SEO ranking. For instance when switching from a CMS to another platform. This will completely change your URL composition. So, to keep your current page with the good SERP position, you can reroute the old URL to the new page.

In this tutorial, we will guide you through creating two kinds of Nginx redirect: permanent and temporary. Remember to have Nginx installed on your VPS.

Temporary and Permanent Nginx Redirect Explained

Temporary redirects are beneficial if a page location needs to change from one place to another location temporarily. The redirects response code 302 is used for designating the temporary movement of a page.

If website maintenance is being performed, temporary redirects are used to notify the users that the website is unavailable. Another example is when you make temporary redirects of an incomplete page; you link that page to another point or the main page:

Visitor–> Website Page–> Website is under maintenance

On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, a 301 redirect is used for designating the permanent movement of a page. These kinds of redirects are helpful when the user wants to change the domain name and no longer wants a browser to access it.

For example, when you wish to change the domain of your website or create a new page for an older one:

Visitor–> Click www.devisers.in/home -> Redirected to www. devisers. in/home1

Page Redirects in Nginx

Remember, first you have to access your VPS through SSH. If you’re having trouble, check out our PuTTY tutorial.

In Nginx, most redirects can be achieved with the help of inbuilt rewrite feature. This is the default feature that’s available on a clean installation of Nginx and can form both kinds of Nginx redirect – i.e. permanent and temporary. In its plain form, it takes a minimum of two cases i.e. old URL and new URL.

It is simple and easy to redirect pages to a temporary or permanent location on Nginx web server. All you need to do is open the /etc/nginx/sites-enabled/default file and paste your preferred redirects.

Nginx Redirecting a Domain

For redirecting one domain to another use the below command in the terminal:

server {
    listen 80;
    listen 443 ssl;
    server_name devisers.in www.devisers.in;
    return 301 $scheme://www.devisers.com$request_uri;
}

Here, we use two domains. The one we want to redirect – www.devisers.in, and the new one  – www.devisers.com.

Nginx Redirect from HTTP to HTTPS (SSL)

HTTP and HTTPS use different ports – HTTP port 80 and HTTPS port 443. Using HTTPS is much more helpful since it protects you from MITM attacks that can hijack your session. Remember, that for this method to work, you need to have an SSL already set up. So, to protect all the information sent between you and your visitors, it is beneficial to redirect all the requests coming from HTTP to HTTPS. For that, we can add this modification to the same file:

server {
    listen 80;
    server_name www.domain.tld;
    return 301 https://www.domain.tld$request_uri;
}

Now all traffic for HTTP default server redirects to HTTPS.

Nginx Redirect Specific Sites

This is essential if you are using various sites or apps and want to redirect only a single site. Follow the steps below:

server { 
listen 80; 
server_name devisers.in;
     return 301 https://devisers.in$request_uri; 
}

Redirect From www to non-www

There are many times when you want your visitors to access the plain domain of your web page such as devisers.in instead of www.devisers.in. Although there are many options to redirect from www to non-www in Nginx, one of the easy way to do so is as described below:

server {
    listen 80;
    listen 443 ssl;
    server_name www.devisers.in;
    return 301 $scheme://devisers.in$request_uri;
}

Important! This is permanent Redirect or “301 Redirect”.

Restart the Nginx web server to put the changes into effect using the command:

sudo systemctl restart Nginx

If you wish to redirect from non-www to www, simply replace the website URL’s mentioned in the above command. Replace www.devisers.in with devisers.in and vice versa.

Conclusion

Nginx is one of the most powerful and easy-to-use web servers which allows you to make temporary and permanent redirects as described above.

Now you know how to create Nginx redirects from HTTP to HTTPS, from www or Non-www or vice versa. Make sure you use the correct redirection types, as incorrect redirections will affect your search rankings. With the help of accurate redirects, you can leverage your current web presence while changing the site structure as required.

We hope this tutorial helps you out! See you in the next one.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.