Nginx is one of the popular web servers used to host websites and applications. We can host multiple sites using the Nginx Web Server by defining each site configuration in its own virtual host placed at the sites-available directory of the Nginx installation. The common path of this directory for Nginx is /etc/
We can also install an SSL certificate from the trusted certificate authorities to ensure that the commonly used browsers and clients recognize the certificate. We can make the website to handle the HTTP (
This tutorial is written for the popular Linux distribution Ubuntu. It provides all the steps required to add redirect rules on Ubuntu 18.04 LTS. The steps should be similar for other Linux systems and Ubuntu versions.
Prerequisites
You must meet the below-listed prerequisites on the server to continue with this tutorial.
Ubuntu Server - This tutorial is written for Ubuntu 18.04 LTS, though it can be any other Linux system. The steps should be the same on other systems. You can also follow Complete Guide To Install Ubuntu 18.04 LTS (Bionic Beaver) to install the desktop version of Ubuntu.
Nginx Web Server - It assumes that the Nginx is already installed on the system and it's configured properly to access it using the IP address. It can be easily done on your local system in case you have a static IP address. You must be able to view the Welcome Screen by simply navigating to http://xx.xx.xx.xx (xx.xx.xx.xx is your IP address pointing to your server) in case the default virtual host is available. You can also follow How To Install And Configure Nginx on Ubuntu 18.04 LTS to install the Nginx Web Server.
Domain - A valid domain properly configured at your domain registrar pointing to your server. I have used example.com in this tutorial for reference. Make sure to replace it with your own domain.
Firewall - Ports 80 and 443 are open to
All Virtual Hosts
This section explains how to catch all the HTTP requests and force them to redirect to https irrespective of any specific website or application. All the sites or applications hosted on Nginx will be forced to use HTTPS. In the previous tutorial, I have explained to configure virtual hosts or server blocks - Configure Virtual Host Or Server Block On Nginx. It can be implemented in case all the server blocks are configured in the same file or the default configuration consist of the catch-all block and the site-specific configuration handles the https request. I will update the default configuration file for the demonstration.
# Update default configuration sudo nano /etc/nginx/conf.d/default.conf # OR sudo nano /etc/nginx/sites-available/default
# Catch All Block - Redirect all HTTP to HTTPS server { # Listen on port 80 as default server listen 80 default_server; listen [::]:80 default_server;
# Match with all the domains server_name _;
# Redirect to https return 301 https://$host$request_uri; }
# HTTPS Specific Blocks
server { listen 443 ssl;
server_name example.com; ... ... }
In this way, we are redirecting all the HTTP requests to HTTPS and listening on 443 for individual sites and applications.
Specific Virtual Host
This section explains how to redirect all the HTTP requests of a specific site or application to HTTPS. We can update the virtual hosts as shown below.
# Update configuration /etc/nginx/sites-available/example.com
# Content server { listen 80;
server_name example.com;
return 301 https://$host$request_uri; }
server { listen 443 ssl;
server_name example.com www.example.com; ... ... }
Summary
This is how we can redirect the requests over the HTTP protocol to the HTTPS protocol.