How to Redirect HTTP to HTTPS in Nginx

Introduction

Before we begin talking about how to Redirect HTTP to HTTPS in Nginx, let's briefly understand - what is Nginx?

Nginx, pronounced "engine x," is a free, open-source, high-performance HTTP and reverse proxy server that helps some of the world's most popular websites to handle their traffic.

You probably deal with Nginx on a frequent basis if you're a developer or a system administrator. Redirecting HTTP traffic to the secure (HTTPS) version of your website is one of the most typical jobs you'll likely conduct.

Unlike HTTP, which sends and receives requests and responses in plain text, HTTPS encrypts the communication between the client and the server using TLS/SSL.

There are numerous advantages to adopting HTTPS over HTTP, including:

  • In both directions, all data is encrypted. As a result, if sensitive information cannot be read if intercepted.
  • Your website will be marked as safe by Google Chrome and all other popular browsers.
  • HTTPS enables you to use the HTTP/2 protocol, which improves site performance dramatically.
  • HTTPS websites are preferred by Google. If you deliver your site through HTTPS, it will rank higher.

Configuring a distinct server block for each version of the site is the best way for redirecting HTTP to HTTPS in Nginx. You should avoid using them if the directive redirects traffic since it can cause the server to behave in unforeseen ways.

In this tutorial, we'll show you how to redirect HTTP to HTTPS in Nginx. We will also address a few FAQs on how to redirect HTTP to HTTPS in Nginx.

Redirect HTTP to HTTPS per Site

When you install an SSL certificate on a domain, you usually get two server blocks for that name. The first is for the site's HTTP version, which uses port 80, and the second is for the HTTPS version, which uses port 443.

Open the domain configuration file and make the following changes to convert a single website to HTTPS:

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

Let's take a look at the code one line at a time:

  • listen 80 - For the chosen domain, the server block will listen on port 80 for inbound connections.
  • server_name vegastack.com www.vegastack.com - Defines the domain names for the server block. Make sure you use your domain name instead.
  • return 301 https://vegastack.com$request_uri- Redirects traffic to the site's HTTPS version. The whole original request URI, including arguments, is stored in the $request_uri variable.

In most cases, you'll want to redirect the HTTPS www version of the site to the non-www version, and vice versa. It's best to construct a separate server block for both www and non-www versions of the redirect.

For example, you could use the following setup to redirect HTTPS www queries to non-www:

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

server {
    listen 443 ssl http2;
    server_name www.vegastack.com;

    # . . . other code

    return 301 https://vegastack.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name vegastack.com;

    # . . . other code
}

For modifications to take effect, you must restart or reload the Nginx service whenever you make changes to the configuration files:

sudo systemctl reload nginx 

Redirect All Sites to HTTPS

You can establish a single catch-all HTTP server block if all the websites hosted on the server are configured to utilize HTTPS and you don't want to create a separate HTTP server block for each site. All HTTP requests will be forwarded to the relevant HTTPS blocks by this block.

Open the Nginx configuration file and make the following modifications to create a single catch-all HTTP block that will redirect users to the HTTPS version of the site:

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name _;
	return 301 https://$host$request_uri;
}

Let's take a look at the code one line at a time:

  • listen 80 default_server - Makes this server block the default (catch-all) block for all domains that aren't matched.
  • server_name - _ is a bogus domain name that never corresponds to a real one.
  • return 301 https://$host$request_uri - Redirect traffic to the matching HTTPS server block with status code 301 (Moved Permanently). The domain name of the request is stored in the $host variable.

If a visitor visits http://example.com/page2, for example, Nginx will redirect the request to https://example.com/page2.

Instead of a worldwide HTTP to HTTPS redirection, create a redirection on a per-domain basis if possible.

FAQs on How to Redirect HTTP to HTTPS in Nginx

What is the benefit of using Nginx to handle HTTP to HTTPS redirection? 

Nginx is a high-performance server that efficiently handles HTTP to HTTPS redirection, minimizing the impact on server resources while ensuring secure connections.

Can I redirect specific URLs or only the entire website from HTTP to HTTPS? 

Yes, Nginx allows you to redirect specific URLs or a subset of your website to HTTPS, giving you flexibility in enforcing secure connections only for certain pages.

Are there any potential issues I should be aware of when redirecting HTTP to HTTPS? 

Yes, it is important to configure the redirects correctly to avoid redirect loops, ensure proper SSL certificates, and consider the impact on any caching mechanisms in place.

Can I redirect HTTP to HTTPS while preserving the original URL structure?

Yes, Nginx allows you to maintain the URL structure when redirecting from HTTP to HTTPS, ensuring that users are directed to the corresponding secure page.

Is it possible to redirect HTTP to HTTPS for multiple domains/subdomains? 

Yes, Nginx supports HTTP to HTTPS redirection for multiple domains and subdomains through separate server blocks or by utilizing the "server_name" directive.

How can I verify if the HTTP to HTTPS redirection is working correctly?

You can verify by accessing your website using HTTP and checking whether it automatically redirects you to the HTTPS version. Additionally, you can use online tools or browser extensions to check HTTP to HTTPS redirection.

Are there any performance considerations when redirecting HTTP to HTTPS in Nginx? 

Properly configured HTTP to HTTPS redirection in Nginx has minimal impact on performance. However, it is recommended to benchmark and optimize your server configuration to ensure optimal performance.

Conclusion

The preferred method of redirecting HTTP to HTTPS in Nginx is to construct separate server blocks and use a 301 redirect.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.