Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

How to redirect HTTP to HTTPS in Nginx

- Advertisement -

Hello, friends. In this short but useful post, you will learn how to redirect HTTP to HTTPS in Nginx. This is to force us to always use a secure protocol.

Is this useful?

Yes, it is. This happens on very exposed websites where we need all connections to go through HTTPS. This ensures that all access is secure and encrypted.

In addition to this, making this change increases the reputation of your site and helps SEO to rank your site higher. So if you are sure you want more security and better results, then you should do it.

Let’s get started.

Prerequisites

Before we continue, we have to make sure that we meet the requirements to complete the post without problems

  • You need to have Nginx installed and configured. So make sure you have it using the official repositories of your Linux distribution.
  • The version of Nginx must be higher than 1.9.5 You should have no issue to have it because most distributions include recent versions.
  • Have basic knowledge on the use of the terminal. A user with sudo access or root access.

Let’s go for it.

How to redirect HTTP to HTTPS in Nginx

Most likely, you have websites configured on the server, so the changes need to be made in the site configuration file.

This would look like this:

sudo nano /etc/nginx/sites-enabled/domain.conf

Inside the server section, and you have to define a code similar to this

- Advertisement -
server {
    listen 80 default_server;
    server_name domain;
    return 301 https://$host$request_uri;
}
redirect HTTP to HTTPS in Nginx
redirect HTTP to HTTPS in Nginx

The content of the section is somewhat explicit but let’s explain it a bit. listen will make the request to listen on port 80. server_name is the domain where the change will be applied. return 301 indicates that it is a permanent redirect. https://$host$request_uri; transforms the request from HTTP to HTTPS.

Save the changes and at the end, close the editor.

Another interesting thing you can do is to redirect only a specific path. For example, /index.html.

For that, the configuration looks something like this

Location /index.html {
   rewrite ^/index.html$ https://domain.com/index.html redirect;
}

So, when you access tohttp://domain.com/index.html, you will be redirected to https://domain.com/incex.html.

Now all that’s left to do is restart Nginx.

sudo systemctl restart nginx

You are done. That’s it. Something so simple can increase the security of the whole server and website.

Conclusion

Redirecting HTTP to HTTPS is a good way to secure a little more the access to your website. By using this methodology, we can get a little more security in Nginx.

I hope you found it useful.

More Ngnix tutorials

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook

x