Redirect all traffic from HTTP to HTTPS using Nginx

Windows Articles

How to Update to MIUI 12 via Fastboot

Greetings friends. The new version of the Android-based operating system MIUI 12 released this year, is still being deployed with some devices missing. On...

How to use modprobe command?

The modprobe command is a Linux administration system created for management, visualization, inclusion and exclusion. From loadable modules to kernel. Also, the Module is...

Install Virtualmin on CentOS 8

Hello, friends. In this post, you will learn how to install Virtualmin on CentOS 8. In addition to that, we will leave it ready...

Different ways to open File Explorer in Windows 10

Hello! Certainly the archive scan is an essential element when it comes to controlling the contents. It is even so important, that there are...

How to setup WireGuard VPN with ubuntu 20.04

Here, we will learn today how to configure WireGuard VPN with Ubuntu 20.04. Wireguard® is an amazingly straightforward yet quick and present-day VPN that...
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 Update to MIUI 12 via Fastboot

Greetings friends. The new version of the Android-based operating system MIUI 12 released this year, is still being deployed with some devices missing. On...

How to use modprobe command?

The modprobe command is a Linux administration system created for management, visualization, inclusion and exclusion. From loadable modules to kernel. Also, the Module is...

Install Virtualmin on CentOS 8

Hello, friends. In this post, you will learn how to install Virtualmin on CentOS 8. In addition to that, we will leave it ready...

Different ways to open File Explorer in Windows 10

Hello! Certainly the archive scan is an essential element when it comes to controlling the contents. It is even so important, that there are...

How to setup WireGuard VPN with ubuntu 20.04

Here, we will learn today how to configure WireGuard VPN with Ubuntu 20.04. Wireguard® is an amazingly straightforward yet quick and present-day VPN that...
x