Methods of payment Abuse

How to redirect from http to https Nginx

22.03.2021, 19:19

Modern trends in the Internet dictate the use of the secure https communication protocol. So when migrating it is important that all traffic that comes to the http port is automatically redirected to https. This is an important condition not only for sites that may lose some traffic, but also for users, so that no one can break an unsecured connection. Next, let's see how to configure redirect from http to https Nginx.

How to make redirect from http to https Nginx

We recommend to use redirect with status code 301.It tells search engines or browser that the current link has been updated and it should be updated in its database, for example, browser bookmarks. If we open the configuration file, we can find two sections server, for https site and http site. In the http section you simply redirect all requests to https using the return instruction, and in the second section you process everything. 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.ru;
root $root_path;
listen :80 default_server;
...
}

How to redirect from http to https Nginx

The second section follows the requests on the 433 port:

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 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.ru;
root $root_path;
listen :443 default_server;
...
}

As you can see, the syntax in this case is very simple and clear. So the return instruction makes it possible to return the necessary server response codes. Thus we return the 301 code, the permanent redirect, and the URL to which we are going to redirect the user. In addition to the return directive, you can use the rewrite instruction, which performs exactly the same actions:

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

In the syntax of regular expressions, you can add the domain instead of return 301. You can also use such a construction without a separate server block:

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

Next, you need to save the file and check the Nginx configuration. For this purpose it is used:

$ sudo nginx -t

настройка Nginx

If everything is OK, restart Nginx:

$ sudo systemctl restart nginx

Check what kind of server response you get with curl:

$ curl -I losst.ru

настройка Nginx

Or let the utility follow the full redirect path:

$ curl ILa losst.ru

настройка Nginx

As you can see, setting up a redirect to https nginx is quite simple, the work is done by adding one line to the configuration file, everything else is additional parameters.