How to Redirect HTTP to HTTPS in Nginx

Introduction

If you are looking to redirect your website from HTTP to HTTPS, then you have come to the right place. In this article, we will discuss how to redirect HTTP to HTTPS in Nginx. Nginx is a popular web server used by many websites and is known for its high performance and scalability. By redirecting your website from HTTP to HTTPS, you can ensure that all of your website traffic is encrypted and secure. We will discuss the steps required to configure Nginx to redirect HTTP to HTTPS, as well as some best practices to ensure that your website is secure.

How to Redirect HTTP to HTTPS in Nginx

1. Open your Nginx configuration file in a text editor.

2. Add the following code to the server block section of your configuration file:

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

3. Save and close the file.

4. Test your configuration file for syntax errors by running the following command:

sudo nginx -t

5. If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Introduction

Nginx (pronounced “Engine-X”) is a Linux-based web server and proxy application. Nginx is a powerful tool for redirecting and managing web traffic. It can be easily configured to redirect unencrypted HTTP web traffic to an encrypted HTTPS server.

This guide will show you how to redirect HTTP to HTTPS using Nginx.

tutorial on forcing redirects from http to https in nginx

Prerequisites

  • A Linux server running Nginx
  • A user account with sudo privileges
  • Remote login to a web server (optional, required only if you are not working directly on your web server)
  • Access to a terminal/command line (Ctrl-Alt-T or Ctrl-Alt-F2)

HTTP to HTTPS Redirect

To enforce an HTTP to HTTPS redirect, you need to edit the Nginx configuration file.

In most cases, you can locate the file in the /etc/nginx/sites-available directory. If not found, search for it here: /etc/nginx/nginx.conf, /usr/local/nginx/conf, or /usr/local/etc/nginx.

Once you have located the Nginx configuration file, open it in a text editor with the command:

sudo nano /etc/nginx/sites-available/server.conf

Replace the location with the actual location and name of your configuration file.

Note: If you are connecting remotely, make sure you’re logged in through SSL first. Also, if you are using a graphical interface, you can browse to the file location instead of using terminal commands.

Once the configuration file is open for editing, insert one of the code blocks below. Once you are finished editing, save the file and exit. Then, restart the Nginx service with the following command:

sudo service nginx restart

Nginx Redirect all HTTP traffic to HTTPS

Open the Nginx configuration file for editing, then insert the following code:

server {

    listen 80 default_server;


    server_name _;


    return 301 https://$host$request_uri;

}

Here is a breakdown of the commands:

  • Listen 80: This instructs the system to catch all HTTP traffic on Port 80
  • Server_name _;: This will match any hostname
  • Return 301: This tells the browser (and search engines) that this is a permanent redirect
  • https://$host$request_uri: This is a short code to specify the HTTPS version of whatever the user has typed

After editing, all traffic for the HTTP default server redirects to HTTPS.

Note: This should be the only server block listening on Port 80. (A server block is a unit of configuration code in Nginx. It’s marked by a name and curly brackets.)

Redirect a Specific Site

You may have multiple servers, but only some of them may require HTTPS. Specify server name in the server block to redirect the selected traffic:

server {

    listen 80 default_server;


    server_name my_app.com;


    return 301 https://my_app.com$request_uri;

}

Replace the name my_app.com with the name of the server you intend to redirect. You may also want to add additional sites by adding another server block. Simply copy the code, and switch out the name of the server.

Accept Only SSL Connections

Add this code to be sure that the server will only accept SSL connections on Port 443:

server {

    listen 443 ssl default_server;

    server_name my_app.com;

}


server {

    listen 443 ssl;

    server_name my_website.com;

}

This code block will set two websites, my_app.com and my_website.com, to accept only SSL connections. You can add additional sites by using additional server blocks.

Nginx Page Redirects

You can use the rewrite code to quickly manage a 301 (permanent) or 302 (temporary) redirect:

Location /index.html {

       rewrite ^/oldURL$ https://www.your_domain.com/newURL redirect;

}

Most of the time, the location will be index.html, but you can specify any path/pattern.

Make note that the rewrite command should only be used with 301 or 302 redirects.

How to Redirect a Domain With Nginx

This is useful if you have changed from a vanity extension (like .biz or .net) to a standard .com address. It can also be used to redirect from an old domain name to a new domain name.

server {

    listen 80;

    listen 443 ssl;

    server_name www.old_company.com;

    return 301 $scheme://www.new_company.com$request_uri;

}

For most instances, the return command is preferred to the rewrite command.

Redirect from www website to non-www website

This process is similar to a standard page redirect:

server {

    server_name www.new_company.com;

    return 301 $scheme://new_company.com$request_uri;

}

Reasons to Redirect Traffic

There are several reasons to redirect HTTP traffic to HTTPS. You may need to:

  • Force a more secure, encrypted connection.
  • Keep a page with good SEO ranking, but send its traffic to a new page.
  • Notify and temporarily send traffic to an “under maintenance” page.
  • Permanently send traffic from one website to another, i.e. after a corporate merger.

Conclusion

Now you know how to redirect HTTP to HTTPS in Nginx. By editing the configuration file, you can send traffic from a specific destination to a different site and enforce the use of Nginx SSL certificates. This helps you safely manage changes to your website without disrupting the user experience.

How to Redirect HTTP to HTTPS in Nginx

If you are running a website on Nginx, you may want to redirect all HTTP requests to HTTPS. This is a good practice for security and SEO purposes. Fortunately, it is easy to do with Nginx.

Step 1: Enable SSL

Before you can redirect HTTP to HTTPS, you need to enable SSL on your server. This involves obtaining an SSL certificate and configuring it in Nginx. For more information on how to do this, please refer to the official Nginx documentation.

Step 2: Add Redirect Rules

Once you have enabled SSL, you can add the following rules to your Nginx configuration file to redirect all HTTP requests to HTTPS:

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

This will redirect all requests to the HTTPS version of your website. You can also add additional rules to redirect specific URLs or paths.

Step 3: Test the Redirects

Once you have added the redirect rules, you should test them to make sure they are working correctly. You can do this by accessing your website using both HTTP and HTTPS and verifying that the redirects are working as expected.

Conclusion

Redirecting HTTP to HTTPS is a simple process with Nginx. All you need to do is enable SSL and add the appropriate redirect rules to your configuration file. Once you have done this, you can test the redirects to make sure they are working correctly.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?