Like most of the Internet sites that follow modern trends, webilicious.xyz uses the secure communication protocol https. Many sites, when switching to https, need all traffic that comes to the http port to be automatically redirected to https.
This is necessary for reasons of SEO optimization, as well as user safety, so that no one can break the secure connection. In this article, we will look at how to set up a redirect from http to https Nginx.
Redirect from http to https Nginx
I am assuming that you will be using a permanent redirect with a 301 status code. This means a permanent move. This method is used to inform search engines or the browser that the current link has been updated and should be updated in its database, for example, browser bookmarks.
There should be two server sections in the Nginx configuration file, for the https site and the http site. In the http section, you simply redirect all requests to https using the return statement, and in the second section, you process everything. For example, the first section:
The second section, already with SSL processing, listens for requests on port 443:
Actually, the syntax is very simple here, the return statement allows you to return the required server response codes. Here 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, with the help of it exactly the same actions are performed:
This is the usual regular expression syntax. In the first argument, we highlight the group of the query string, and in the second we specify the correct domain. You can add it instead of return 301. You can also use this construct without a separate server block:
Now it remains to test what happened. Save the file and check the nginx configuration:
sudo nginx -t
Then, if all is well, restart Nginx:
sudo systemctl restart nginx
Next, you can check what kind of server response you get using curl:
curl -I webilicious.xyz
Or let the utility follow the full path through the redirection:
curl ILa webilicious.xyz
Setting up a redirect to https nginx is quite simple, in fact, everything is done by adding one line to the configuration file, everything else is additional parameters. The redirect from https to http nginx will be done in the same way. You just need to change a few letters in the retrun parameters. Do not forget to check if the redirects from www and to https are configured correctly, this plays a very important role in SEO. Hope this information was helpful to you.