How can you configure Nginx to handle server-side redirects? nginx

How can you configure Nginx to handle server-side redirects?

Nov. 13, 2023

How can you configure Nginx to handle server-side redirects?

Nginx is a popular web server software that is known for its high performance and scalability. It also provides powerful features to handle server-side redirects, which are useful for redirecting users from one URL to another. In this article, we will explore how to configure Nginx for server-side redirects.

Step 1: Install Nginx

If you haven't already installed Nginx, you can do so by following the instructions for your specific operating system from the official Nginx website.

Step 2: Edit Nginx Configuration

After installing Nginx, locate the main configuration file, usually named nginx.conf, which is typically located in the /etc/nginx/ directory on Linux systems.

Open the file in your preferred text editor and locate the server block that corresponds to the domain or virtual host you want to configure the redirect for.

Step 3: Configure the Redirect

Inside the server block, add a new location block to define the URL path that you want to redirect. For example, if you want to redirect all requests to "" to "", the configuration would look like this:

    location /old-url {
        return 301;

Here, we use the return directive with a 301 status code to perform a permanent redirect. You can also use other redirect codes if needed, such as 302 for temporary redirects.

Step 4: Save and Test the Configuration

Save the changes to the Nginx configuration file and restart the Nginx service to apply the changes. On most Linux systems, you can use the following command:

    sudo service nginx restart

To test if the redirect is working as expected, try accessing the old URL in a web browser. It should automatically redirect you to the new URL defined in the configuration.


Nginx provides a simple yet powerful way to handle server-side redirects. By following the steps outlined in this article, you can easily configure your Nginx server to redirect users from one URL to another, ensuring a smooth user experience and maintaining SEO optimization.