Redirect all traffic from HTTP to HTTPS using Nginx

Windows Articles

How to install Redis on CentOS 8?

Hello, friends in this post, we will show you how to install Redis on CentOS 8 As we well...

How to enable and disable startup applications in Windows 10

Hello! Windows 10 is an attractive and very easy to use system. However, some criticism has come for being too "heavy". That...

How to install Oracle Java 15 on Ubuntu 20.04 / 18.04?

Hello, friends. We know that Java is one of the most popular programming languages and now it's version 15. So in this...

How to run the Linux Tail command in Windows 10.

Hi! The title of this post may seem incongruous, but it's not. In fact, for some time Microsoft has approached positions with...

How to compress large videos in Windows 10

Hello, how are you? Multimedia resources are definitely used in any system. Indeed, we live in the age of technology. That's why...
angeloma
angeloma
Senior Writer and partner

Hello, friends. In this post, you will learn how to redirect HTTP traffic to HTTPS using the Nginx web server. Previously we did it with Apache but now it’s the turn of the great competitor Nginx.

On a web server, it is always convenient to have access via HTTPS to ensure that all communication is secure. Except for a very few cases on private servers without internet access is that you can afford not to have HTTPS.

So, if a user explicitly writes http:// what we will do is redirect him to https:// with this we will avoid security leaks.

Redirect all traffic from HTTP to HTTPS for one single site

Each site we have in Nginx will have its configuration. So it’s possible to redirect all traffic from HTTP to HTTPS in a single site.

To do this, access the site’s configuration file and edit it. Usually, the path is /etc/nginx/sites-available/ and it will be there with the name you have given it. For example:

sudo nano /etc/nginx/sites-available/site_conf_file

And within the configuration, it is necessary to add

server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://your domain.com$request_uri;
}

This is not enough because now we have to add the configuration of the SSL certificate that secures the HTTPS connection.

Below add the following

server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate yourdomain.com.crt;
ssl_certificate_key yourdomain.com.key;
}

Remember that you have to change yourdomain.com for yours. In addition to having SSL configured.

A good practice is also to redirect HTTPS connections that start with www. To do this, add the following

server {
listen 443 ssl;
server_name www.yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}

Save the file and exit the editor.

To apply the changes, simply restart the Nginx service with systemctl.

sudo systemctl reload nginx

And it should already work on your site.

Redirecting for all sites

Although each site has a particular configuration, many times it is convenient to do this configuration globally. So that it affects all the sites hosted on the server.

To do this, you only need to add this in the main configuration file of Nginx.

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

Then save your changes and exit the editor.

Again, restart Nginx.

sudo systemctl reload nginx

That is it.

Conclusion

Redirecting all traffic to HTTPS using Nginx may be thought to be difficult but here we have shown you that it is not. Definitely a web server that has an outlet to the Internet has to always try to handle these connections.

So, share this post and join our Telegram Channel.

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How to install Redis on CentOS 8?

Hello, friends in this post, we will show you how to install Redis on CentOS 8 As we well...

How to enable and disable startup applications in Windows 10

Hello! Windows 10 is an attractive and very easy to use system. However, some criticism has come for being too "heavy". That...

How to install Oracle Java 15 on Ubuntu 20.04 / 18.04?

Hello, friends. We know that Java is one of the most popular programming languages and now it's version 15. So in this...

How to run the Linux Tail command in Windows 10.

Hi! The title of this post may seem incongruous, but it's not. In fact, for some time Microsoft has approached positions with...

How to compress large videos in Windows 10

Hello, how are you? Multimedia resources are definitely used in any system. Indeed, we live in the age of technology. That's why...
x