Redirect from http to https with Nginx

Like most of the Internet sites that follow the modern trends losst.ru uses a secure communication Protocol https. Many sites when switching to https it is necessary that all traffic that comes to the http port are automatically redirected to https.

This is necessary for reasons of SEO optimization, and user security so that no one could break a secure connection. In this article we will discuss how to set up a redirect from http to https with Nginx.

Redirect from http to https with Nginx

I assume you will use a permanent redirect with status code 301. This means moving on a regular basis. This method is used to tell search engines or browser that the current reference has been updated and it is updated in its database, for example, your browser bookmarks.

In the configuration file Nginx should be two section server for your website to https and http. In the section http you just redirect all requests to https by using the return statement, and the second section for all process. For example, the first section:

server {
server_name losst.ru www.losst.ru;
charset off;
index index.php;
ssi on;
return 301 https://$host:443$request_uri;
set $root_path /var/www/losst/data/www/losst.EN;
root $root_path;
listen :80 default_server;
...
}

The second section is already processing requests SSL listens on port 443:

server {
server_name losst.ru www.losst.ru;
ssl on;
ssl_certificate "/var/www/losst/losst.ru_le2.crtca";
ssl_certificate_key "/var/www/losst/losst.ru_le2.key";
ssl_ciphers directives EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
add_header Strict-Transport-Security "max-age=31536000;";
charset off;
index index.php;
set $root_path /var/www/losst/data/www/losst.EN;
root $root_path;
listen :443 default_server;
...
}

Actually, the syntax is very simple, the return statement allows to return the appropriate response codes of the server. Here we return the code 301, a permanent redirect, and the URL going to redirect the user. In addition to the return Directive, you can use the manual rewrite, it performs exactly the same steps:

rewrite ^/(.*)$ https://losst.com/$1 permanent;

This is the usual regular expression syntax. The first argument we have allocated group of the query string, and secondly you specify the correct domain. It can be added instead of return 301. You can also use this design without a separate server block:

if ($host ~* ^(losst.ru/www.losst.ru)$ ){
rewrite ^/(.*)$ https://losst.ru/$1 permanent;
}

Now we have to test that out. Save the file and check the configuration of nginx:

sudo nginx -t

Then, if all is well, restart Nginx:

sudo systemctl restart nginx

You can then check what the server response you will get using curl:

curl-I losst.ru

Or allow the tool to follow the complete path to redirect:

curl-ILa losst.ru

Insights

To set up a redirect to https with nginx is quite simple, actually, all done by adding a single line in the config file, everything else. Redirect from https to http with nginx will run as. Only need to change a few letters in the parameters retrun. Don’t forget to check whether configured with www and redirects to https, it plays a very important role for SEO. I hope this information was useful for you.

Source: losst.ru

(Visited 34 times, 1 visits today)