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.

Download Complete Linux Commands Cheat Sheet

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, the redirects response code 301 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 -> 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. In the location /etc/nginx/sites-enabled/default you should paste in the following code, while changing the variables to suit your needs:

Location path_pattern {        
     rewrite ^/oldURL$ redirect; 

If you want to redirect the page to another link permanently, simple use “permanent” instead of “redirect” in the above command. Meanwhile, the path_patern is typically /index.html.

Nginx Redirecting a Domain

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

server {
      listen 80;
      rewrite ^$request_uri? permanent;

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

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 default_server;
server_name _;
return 301 https://$host$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; 
     return 301$request_uri; 

Here, we are redirecting the site to

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 instead of 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 {
    return 301 $scheme://$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 with and vice versa.


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.

Was this tutorial helpful?

The author

Edward S.

Edward is a Content Editor with years of experience in IT as a writer, marketer, and Linux enthusiast. Edward's goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.