25.5 C
Texas
angeloma
Senior Writer and partner

Redirect all traffic from HTTP to HTTPS using Nginx

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.

- Advertisement -

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.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article