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

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

In our example, the Nginx server is hosting the website WWW.GAMEKING.TIPS.

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 HTTP to HTTPS

Install the Nginx server.

Copy to Clipboard

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Add the following line to the configuration file.

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 all HTTP requests to HTTPS.

From a remote Linux computer, try to perform an HTTP access.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

In our example, if a user tries to access the HTTP version of any page, he will be redirected to the HTTPS version of the same page.

Make sure that you have an HTTPS website configured on the Nginx server or the connection will be lost.

As an example, here is an Nginx configuration file with HTTP and HTTPS enabled.

Copy to Clipboard

Congratulations! You successfully configured the HTTP to HTTPS redirection on the Nginx server.