Redirect from http to https Nginx
0

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:

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

The second section, already with SSL processing, listens for requests on port 443:

server {
server_name webilicious.xyz www.webilicious.xyz;
ssl on;
ssl_certificate "/var/www/Webilicious/webilicious.xyz_le2.crtca";
ssl_certificate_key "/var/www/Webilicious/webilicious.xyz_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/Webilicious/data/www/webilicious.xyz;
root $root_path;
listen :443 default_server;
...
}

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:

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

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:

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

Now it remains to test what happened. Save the file and check the nginx configuration:

sudo nginx -t

Redirect from http to https Nginx 3

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

Redirect from http to https Nginx 4

Or let the utility follow the full path through the redirection:

curl ILa webilicious.xyz

Redirect from http to https Nginx 5

conclusions

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.

Install Armbian on Orange Pi Zero Plus2

Previous article

Installing Nagios on Ubuntu | Webilicious

Next article

You may also like

Comments

Leave a Reply

More in Linux