NGINX: How to forward all requests to HTTPS

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction to HTTPS Redirection

Securing website traffic is a crucial aspect of web administration and a fundamental responsibility for developers. Security not only ensures the privacy and integrity of data but also boosts user trust. HTTPS is the secure version of HTTP, and it employs encryption protocols like SSL/TLS to protect data in transit between clients and servers. This tutorial will guide you through the steps of redirecting all HTTP requests to HTTPS using NGINX, a popular open-source web server software.

Understanding the Importance of HTTPS

Before delving into technical implementation, let’s discuss the importance of employing HTTPS protocols. When a website uses HTTPS, it provides three key layers of protection:

  • Encryption: HTTPS encrypts the exchanged data to keep it secure from eavesdroppers. This means that while the user is browsing the website, nobody can ‘listen’ to their conversations, track their activities, or steal their information.
  • Data Integrity: Data cannot be tampered with or corrupted during transfer, intentionally or unintentionally, without being detected.
  • Authentication: It proves that your users communicate with the intended website. It’s essential for building users’ trust and ensuring a strong brand identity.

Prerequisites

  • Access to an NGINX web server
  • A registered domain name
  • A valid SSL certificate

Note: This tutorial assumes that you have sudo or root privileges for making changes to the server configuration.

Getting Started with NGINX Configuration

The NGINX configuration file is usually found at /etc/nginx/nginx.conf or within the /etc/nginx/sites-available/ directory, depending on the distribution and the way NGINX was installed. If you’re using separate configuration files for each domain (as recommended for maintainability), you would edit the conf file corresponding to your domain.

Step 1: Create a Server Block for HTTPS

Before setting up a redirect, ensure that you have a server block that listens on port 443 for HTTPS connections. This block should have the SSL certificate and key specified.

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /path/to/your/fullchain.pem;
    ssl_certificate_key /path/to/your/privatekey.pem;

    ... # Additional directives
}

Step 2: Redirecting HTTP to HTTPS

The following NGINX configuration snippet demonstrates a general-purpose HTTP to HTTPS redirect:

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

    return 301 https://$host$request_uri;
}

This server block listens on port 80 (the standard port for HTTP traffic) and includes a return directive that permanently redirects the client to the HTTPS version of the URL.

Step 3: Server Block Detail Explanation

Let’s break down the redirect server block in detail:

  • listen 80;: Instructs NGINX to listen for incoming connections on port 80.
  • server_name: Defines which domains should respond to within this server block.
  • return 301 https://$host$request_uri;: Issues a permanent redirect status (301), telling the client to make future requests using HTTPS. The variables $host and $request_uri ensure that the original hostname and path are preserved while redirecting.

Optimizing SSL/TLS Settings

Beyond simply redirecting to HTTPS, it’s crucial to optimize your SSL/TLS settings for security and performance. You can define SSL protocols in your server block:

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;

Testing and Reloading NGINX

After editing the configurations, it’s critical to test the changes to ensure there are no configuration errors:

sudo nginx -t

If NGINX reports that the syntax is ok and the configuration file test is successful, you can proceed to reload the service:

sudo systemctl reload nginx

This will apply changes without dropping connections.

Conclusion

Redirecting HTTP to HTTPS is a simple but effective measure to enhance the security of your website. While this tutorial gives you a basic configuration to achieve this with NGINX, remember that keeping your server secure involves maintaining up-to-date SSL certificates and staying abreast of the latest security best practices. By effectively leveraging NGINX features, you can ensure your website safeguards user data while adhering to modern security standards.