Would you like to learn how to redirect a URL on Nginx? In this tutorial, we are going to show you how to install the Nginx server and create a rule to redirect a specific URL on a computer running Linux.

• Ubuntu 18
• Ubuntu 19
• Ubuntu 20
• Nginx 1.18.0

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Nginx - Redirect a URL

Install the Nginx server.

Copy to Clipboard

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Add the following line to create a permanent URL redirection using code 301.

Copy to Clipboard

Add the following line to create a temporary URL redirection using code 302.

Copy to Clipboard

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

In our example, the Nginx server will redirect the TEST1 URL to the Google website using code 301.

In our example, the Nginx server will redirect the TEST2 URL to the Google website using code 302.

Code 301 is a permanent URL redirection.

Code 302 is a temporary URL redirection.

From a remote computer, test the URL redirection configured on the Nginx server.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Congratulations! You are able to configure a URL redirect on the Nginx server.