Redirecting from HTTP to HTTPS in NGINX

3 min readFeb 4, 2023

In today’s world of increased security awareness, it’s important to ensure that your website traffic is encrypted to protect the privacy and security of your visitors. One way to do this is by redirecting all HTTP traffic to HTTPS. In this article, we’ll show discuss how to configure a redirect from HTTP to HTTPS using NGINX, one of the most popular web server software.

Prerequisites

Before we get started, you’ll need to have the following:

  • A working NGINX installation
  • An SSL/TLS certificate for your domain (this can be obtained from a certificate authority such as Let’s Encrypt)

Configuring the Redirect using return 301:

NGINX uses a configuration file to manage its settings. To configure the redirect, you’ll need to edit this file and add the following code:

server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

This code listens on port 80 (HTTP) and when a request is received, it returns a 301 redirect to the same URL but with the HTTPS scheme. The $host variable contains the hostname of the requested URL, and the $request_uri variable contains the rest of the requested URL.

Redirecting based on conditions with if:

The if statement in NGINX allows you to specify conditions for redirecting traffic. For example, you might want to redirect all traffic from http://www.example.com to https://example.com, but only if the request is not already being served over HTTPS. Here's how you could do this using an if statement:

server {
listen 80;
server_name www.example.com;

if ($scheme != "https") {
return 301 https://example.com$request_uri;
}
}

In this example, the if statement checks the value of the $scheme variable, which contains the request scheme (HTTP or HTTPS). If the request is not already being served over HTTPS, the code inside the if statement is executed and a 301 redirect is returned to https://example.com$request_uri.

Redirecting using rewrite

The rewrite directive in NGINX allows you to rewrite URLs in a more flexible and powerful way than using a simple redirect. For example, you might want to redirect all traffic from http://example.com/old-page to https://example.com/new-page. Here's how you could do this using a rewrite directive:

server {
listen 80;
server_name example.com;

if ($scheme != "https") {
rewrite ^/old-page$ https://example.com/new-page permanent;
}
}

Testing the Redirect

Once you’ve saved the changes to your NGINX configuration file, you’ll need to test the redirect to make sure it’s working as expected. To do this, simply visit your website using the HTTP scheme (e.g. http://example.com) and verify that it redirects to the HTTPS version of your site (e.g. https://example.com).

Conclusion

Redirecting from HTTP to HTTPS is an important step in securing your website traffic and protecting your visitors’ privacy. With NGINX, it’s easy to configure the redirect using just a few lines of code. By following the steps outlined in this article, you can ensure that all of your website traffic is encrypted and secure.

I am a DevOps engineer, who writes about IT, career, and finance advice, follow me for more interesting articles, you can check my website for more: it-nuggets.com

https://www.linkedin.com/in/ladouibilal/

--

--

Ladoui Bilal
Ladoui Bilal

Written by Ladoui Bilal

Passionate DevOps engineer with passion for IT and Self Improvement , Chekc my Blog : https://it-nuggets.com

Responses (1)