paint-brush
Redirect HTTP Traffic To HTTPS On An NGINX Serverby@zfrqbl
136 reads

Redirect HTTP Traffic To HTTPS On An NGINX Server

by RedSwitchesAugust 27th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Learn how to redirect HTTP to HTTPS in the NGINX server for improved security and privacy from this comprehensive guide.
featured image - Redirect HTTP Traffic To HTTPS On An NGINX Server
RedSwitches HackerNoon profile picture

HTTPS has become a standard for a reliable web presence.


In most cases, HTTPS websites are preferred in Google rankings and are generally perceived as secure (and therefore trusted) by users.


That’s why you need to ensure that all HTTP traffic coming to your servers is redirected to HTTPS, so you can continue to enjoy the trust of your visitors and search engines.


In this quick tutorial, I will show you how to redirect all HTTP traffic hitting your NGINX server to HTTPS. I will go straight to the required steps and then discuss why this is such a critical step in securing your NGINX server and promoting your brand for better reach and profitability.

How to Redirect HTTP Traffic to HTTPS on NGINX

Before diving into the details, make sure you have the following:

  • A Linux server with NGINX installed (I recommend a bare metal 10Gbps server).
  • A user account with sudo privileges or root access.
  • Access to the command line/terminal.


You need a valid SSL certificate and key to enable HTTPS connections

Step #1: Edit the NGINX Configuration File & Add HTTPS Server Block(s)

The first step of the process is to edit nginx.conf, the NGINX configuration file. If you have a default NGINX installation, this file is usually found in the /etc/nginx/ directory.


Open the configuration file in your favorite text editor. I will use Vi for this demonstration.


# sudo vi /etc/nginx/nginx.conf


Now, scroll down to server block(s) section in the file and add a new block that listens for secure SSL connections on port 443 (the standard port for HTTPS traffic):

server {
    listen 443 ssl default_server;
    server_name [your_domain];
}


Remember to replace [your_domain] with the URL of your website


The server block should look like this in the file:


If you have multiple sites hosted on the server, I suggest replicating this server block for each hosted site. Here is a screenshot illustrating this idea:



Step #2: Set Up The Redirection

After adding the server blocks to the configuration file, you can now set up the redirection.


There are two scenarios in this context: you can redirect the HTTP traffic to HTTPS for a single site or for all the websites hosted on the NGINX server.


Let’s discuss these scenarios.


Redirect All Websites to HTTPS


This is often the preferred scenario for most brands.


You can achieve this redirection by adding either the RETURN or REWRITE directives. Let’s start with the RETURN directive.


The RETURN directive is often used to setup redirection to error pages. When NGINX encounters this directive, it stops the processing of the request and “returns” the response in the sends the response mentioned in the block.


Open the nginx.conf file in your preferred editor and add the following block:


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


In this block, theRETURN directive returns HTTP status 301 (Moved Permanently) and then the new URL for the HTTPS version.


Alternatively, you can use the REWRITE directive that does not set up a redirection. Instead, when NGINX encounters the REWRITE directive, it does not stop processing the request. Instead, it modifies the request URL based on the directive and continues processing the request.


Here is how the server block looks like for a REWRITE directive:


server {
    listen 80 default_server;
    server_name _;
    rewrite ^/[old-page]$ https://[domain]/[new-page] permanent;
}


You can see that the directive rewrites the incoming request and changes the URL to HTTPS.


Here is how the REWRITE directive looks like for an example domain:


Remember to save the file and exit the editor.


Finally, restart the NGINX service to reload the new configuration. I suggest running the following command in the terminal:


service nginx restart


Redirect a Single Website

You can use the REWRITE or RETURN directives to redirect HTTP traffic to HTTPS for a specific site. You should carefully check the domain in the directive to ensure proper redirection and avoid problems for your visitors.


Wrapping Up

Securing your website with HTTPS is no longer just an option—it’s a necessity for building trust with users and improving your search engine rankings. By implementing HTTP to HTTPS redirection on your NGINX server, you ensure that all traffic to your site is secure, boosting your site's credibility and protecting your visitors' data.


In this guide, I covered how to use both REDIRECT and REWRITE directives to efficiently redirect HTTP traffic to HTTPS. By following these steps, you’re not only safeguarding your web presence but also positioning your brand for better visibility and success.


Taking the time to configure your server correctly will pay off in terms of user trust and SEO benefits. Now that you have these tools in your toolkit, your website is well on its way to being secure and fully optimized for today’s internet standards.


FAQs


Q. Where can I find the NGINX configuration file to set up the HTTP to HTTPS redirection?

The NGINX configuration file is typically located in the NGINX directory, usually at /etc/nginx/nginx.conf. This file is crucial for making changes to the server configuration, including setting up HTTP to HTTPS redirection.


Q. What is the difference between using a single server block and extra server blocks for HTTPS redirection?

A single server block can handle the redirection for one domain, while extra server blocks are used to manage redirection for multiple domains. Each server block will have its own server_name directive to specify the domain it handles, making server configuration more organized.


Q. Why is the redirect directive important in the NGINX configuration file?

The redirect directive, such as return 301 https://$host$request_uri;, is used to permanently redirect HTTP traffic to HTTPS. This is a common task in server configuration to enhance security, ensuring that login credentials and other sensitive information are encrypted.


Q. What should I do if I encounter browser error messages after setting up redirects?

If you experience browser error messages after configuring simple redirects, check the error log in the nginx config directory. Common errors may include misconfigured directives or syntax issues in the configuration file. Reviewing the error log will help you identify and correct the issues, ensuring the right response to incoming requests.


Q. How can I handle common errors related to direct HTTP traffic in NGINX?


Common errors related to direct HTTP traffic can be resolved by properly configuring the NGINX server. Ensure that your server block accurately handles incoming requests and directs them to HTTPS. If you encounter error messages, check the error log for details and correct the configuration as needed.


Q. What are some common errors and their solutions when configuring NGINX redirects?

Common errors when configuring NGINX redirects include syntax errors in the nginx config file and incorrect server block directives. These can lead to browser error messages and improper handling of incoming connections. Always check the error log to diagnose issues and ensure that the configuration file is properly set up to handle normal traffic and direct HTTP traffic to HTTPS.


Q. How can using a certificate authority improve application performance and security in NGINX and Apache?

Using a certificate authority to issue SSL/TLS certificates ensures that your website's HTTPS connections are trusted and secure. This improves application performance by enabling secure HTTPS connections, boosts user trust, and complies with best security practices. NGINX and Apache can be configured to use certificates from a certificate authority to enhance security and performance.


Q. What should I do if I encounter a network error when setting up NGINX - Redirect?

If you encounter a network error when configuring NGINX, (NGINX - Redirect), it may be due to the nature of syntax errors in your configuration file or issues with port connections. Ensure you have appropriately configured the ports and check for any syntax errors by running sudo nginx -t to test the configuration. If there are errors, use sudo nginx to review and correct them before restarting the server.


Q. How can I manage redirects on NGINX for multiple domains on a single host server?

To manage redirects for multiple domains on a single host server, set up separate configurations for each domain. This ensures that each domain is appropriately handled and redirected, preventing conflicts.