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:
The second section is already processing requests SSL listens on port 443:
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:
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:
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:
Or allow the tool to follow the complete path to redirect:
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.