Ngnix is one of the most widely used webservers & it serves around 33% of the websites in the world. We have also discussed how we can install the Nginx web server & configure virtual hosts, secure it with SSL certificate using Let’s Encrypt & also how we can use it as a reverse proxy to secure & serve other applications.

In this tutorial, we are going to discuss how we can redirect HTTP to HTTPS in Nginx i.e. how we can secure our website by redirecting all unsecured traffic on port 80 to SSL secured port 443.

There are plenty of reasons why we would want to do this, the major one being all our data will be secured with encryption. So even if network packets are captured by someone then they won’t be able to read the data at all. Other advantages being we can use http/2, improved website rankings with Google and other search providers, etc.

Recommended Read:  Redirect http to https : Apache Server

Also Read: Easy way to integrate Apache with modsecurity on Ubuntu

Now let’s discuss how we can redirect HTTP to https in Nginx.

Redirect HTTP to HTTPS in Nginx

Considering how we have set up our Nginx server, there are two ways we configure this,

1- Redirecting all HTTP traffic to HTTPS,

2- Redirecting HTTP traffic for a single virtual host (website).

But before you make any change for this, you need to make sure that you have a valid SSL certificate configured in nginx. Now, let’s discuss this one by one,

1- Redirecting all HTTP traffic to HTTPS

Open the Nginx configuration file, (normally its /etc/nginx/nginx.conf or it can also be /etc/nginx/conf/default.conf or /etc/nginx/conf/virtual.conf). Now under the section for port 80 for Nginx configuration, edit the file with following,

server {

listen 80 default_server;

server_name _;

return 301 https://$host$request_uri;


Save the file & restart the nginx service to implement the changes. Now let’s discuss if we have multiple websites configured on a single Nginx server.

2- Redirecting HTTP traffic for a single virtual host (website)

To make a change to a single virtual block/ website, open configuration file and make the following change to the website for which you need to redirect http to https in nginx,

server {

listen 80;


return 301$request_uri;


Restart the nginx & we are done. This also concludes our tutorial on how to redirect http to https in ngnix with simple configurations. Please do let us know if you have any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.