How to Redirect a URL on NGINX

How to Redirect a URL on NGINX

Are you searching for an effective way to manage and control traffic to your website? 

Perhaps you need to redirect your users to a different website address. 

If you’re specifically interested in mastering the art of URL redirection using NGINX, then you’ve come to the right place!

This article provides a comprehensive tutorial that guides you through the process of URL redirection using NGINX, ensuring you have all the information you need.

Without wasting any time, let’s jump right in.

What Is the Need for Redirecting a URL on NGINX?

URL redirection plays a vital role in ensuring a seamless user experience by automatically directing users from the previous URL to the new one. 

Here are some key use cases for URL redirection on NGINX:

  • It helps change the domain names, maintain the online presence’s continuity, and avoid broken links.
  • It provides the ability to handle HTTP to HTTPS redirection.
  • Eases the process of handling non-www to www transitions or vice versa.
  • Manage and maintain website changes through URL restructuring.
  • Ideal for handling temporary maintenance or downtime.

Continue reading to learn about prerequisites needed to redirect a URL on NGINX. Also follow the detailed steps provided for a seamless redirection of URL on NGINX.

What You’ll Need

Before we proceed with learning how to redirect a URL on Debian, ensure you have the following prerequisites:

  • Root or sudo access to your server (guide).
  • Have a reliable internet connection.
  • NGINX installed (Latest preferred) and running on your server (guide).
  • Basic understanding of Linux terminal commands and NGINX configuration (guide).

If you have all the above requirements, continue reading and learn how to redirect URLs on NGINX by following the steps below.

How to Redirect a URL on NGINX: Step-by-Step

Step 1: Install NGINX

  1. In order to redirect or handle URLs using NGINX, it’s necessary to have NGINX installed on your Linux Distribution. To install NGINX, you can use the following command:
sudo apt install nginx
redirect a url on nginx 1
  1. Even with a successful installation, the changes won’t reflect instantly. It is advised to restart your device and validate which version of Nginx is installed on your system using the following command:
    sudo nginx –v
    redirect a url on nginx 2

    Step 2: Update and Upgrade NGINX

    If you have installed and set up NGINX a while ago, it is considered a best practice to update NGINX before making any modifications. Updating ensures that the local package list on the system is refreshed, enabling uninterrupted and seamless progress.

    1. To update NGINX, execute the following command:
    sudo apt update
    redirect a url on nginx 3
    1. Like updating NGINX, it is mandatory to upgrade the NGINX server by installing updates or bug fixes when available. This allows the server software to be updated with the newest functions and security patches. Use the following command to upgrade NGINX:
    sudo apt upgrade nginx
    redirect a url on nginx 4
    1. The procedure will begin only after you confirm by entering ‘Y‘ when prompted for both updating and upgrading NGINX.

    Note: The updating of NGINX depends on your operating system and varies according to the Linux distributions. As we have considered Ubuntu, we have used the apt package manager.

    If you’ve updated and upgraded NGINX successfully, you’re ready to make modifications to the NGINX Config file. Continue reading to learn how to modify the config file.

    Step 3: Open NGINX Config File

    1. Open the NGINX configuration file by accessing the server where NGINX is installed. Typically, the file can be found in the /etc/nginx/ directory. Although there is a single file for each domain in the /etc/nginx/sites-available/ directory, the main configuration file is called nginx.conf. Use the following command to open the configuration file (you can use any text editor of your choice for this purpose):
    sudo vi /etc/nginx/nginx.conf
    redirect a url on nginx 5
    1. (Optional) However, if your website has separate configured virtual hosts such as /etc/nginx/sites-enabled/domain.conf, then open it using the following command:
    sudo vi /etc/nginx/sites-enabled/domain.conf

    Step 4: Multiple Ways to Redirect URL on NGINX

    Considering URLs can be used for different purposes on NGINX, you can redirect URLs on NGINX in multiple ways. Let’s discuss a few scenarios of URL redirection and how to implement them.

    Note: The server block provided for each must be added in your configuration file’s following-server block section.

    1. Temporarily Redirect URL to Another URL on NGINX

    Users are transferred from the original URL to the new URL through a temporary redirect in NGINX, informing browsers and search engines that the redirection is only temporary (via HTTP status code 302).

    This means that instead of keeping the redirection, the browser will continue to access the original URL during subsequent sessions.

    server {
    # Temporary redirect to an individual page
    rewrite ^/old-url$ http://www.domain.com/new-url redirect;
    }
    

    2. Permanently Redirect the URL to a New One

    Users are transferred from the original URL to the new URL by a permanent redirect, denoted by an HTTP status code of 301. For subsequent requests, browsers and search engines update their cached data to replace the old URL with the new URL.

    server {
    # Permanent redirect to an individual page
    rewrite ^/old-url$ http://www.domain.com/new-url permanent;
    }
    

    3. Permanently Redirect URL to Its Non-WWW Version

    By switching from the “www” version of the URL to the “non-www” version and using a 301 HTTP status code to indicate that this is a permanent change, this redirect consistently provides the URL of your website.

    Since search engines view “www” and “non-www” versions as independent websites, it is crucial for SEO.

    server {
    # Permanent redirect to non-www
    server_name www.domain.com;
    rewrite ^/(.*)$ http://domain.com/$test permanent;
    }
    

    4. Permanently Redirect URL to Its WWW Version

    This is opposite to permanently redirecting a URL to its non-www version, as it redirects and sends users who access the ‘non-www’ version of a URL to the ‘www’ version, with a 301 HTTP status code signaling that this redirection is permanent.

    This ensures a consistent website URL and prevents search engines from seeing duplicate content.

    server {
    # Permanent redirect to www
    server_name domain.com;
    rewrite ^/(.*)$ http://www.domain.com/$test permanent;
    }
    

    5. Redirect URL to New Location

    This technique allows a specific page or directory URL to be redirected to a new location, whether it’s another page on the same site or a completely different website. The 301 HTTP status code is typically used here to tell browsers and search engines that the original URL permanently moved to the new location.

    server {
    # Permanent redirect to new URL
    server_name domain.com;
    rewrite ^/(.*)$ http://newdomain.com/$1 permanent;
    }
    

    These are some key ways to redirect URLs on NGINX, choose the one that is needed for your website. Before you start putting the changes to work, check the syntax and restart NGINX.

    Also Read: How to Create a Reverse Proxy in Nginx

    Step 5: Check Syntax and Restart NGINX

    1. After making modifications to the config file, it is mandatory to check the syntax to ensure no errors. Use the following command to test the syntax:
    sudo nginx –t
    1. If there are no syntax issues, you will know the test is successful as shown in the screenshot below. If it fails, edit and save the configuration file properly without syntax mistakes.
    redirect a url on nginx 6
    1. Ensure that the configuration passes the test. Once done, restart the device using the following command for the changes to reflect:
    sudo systemctl restart nginx

    Conclusion

    Congratulations on successfully setting up a URL redirect using NGINX!

    NGINX is a powerful tool for redirecting URLs, and the best part is that it can be done through simple configuration changes. Remember to validate your configuration before restarting NGINX to minimize any potential downtime.

    If you encounter any issues or challenges along the way, don’t hesitate to seek help from the supportive NGINX Community Forum. It is an excellent resource for finding answers to errors and warnings specific to NGINX.

    Related Posts