A guide to redirecting HTTP to HTTPS in Nginx

22 May 2019


In this article, I will explain how we can redirect HTTP traffic to HTTPS using Nginx's reverse proxy.

Ideally, you'll need an SSL certificate that has been validated by a CA (or Certificate Authority) in order for this to work.

A bit about Nginx

Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX uses a very small memory footprint, in comparison with Apache and scales in all directions.

The example below will presume that you have a web server up and running on port 80

But first, we will need to access our config file. If you are using Ubuntu or any other linux distro, you'll most likely find this under /etc, providing you have nginx installed on your server.

I will be accessing the config file using vi shell editor:

Upon opening the file, your config may well look something like this:

Create our new server block for port 443:

As a sweetener, we'll be adding some extra directives to optimise HTTPS by specifying what cipher suites we will be using (credit to Bjorn Johansen's "Optimizing HTTPS on Nginx" for this solution)

Our config should now be looking something like this: