How to redirect site from HTTP to HTTPS in nginx load balancer

1) Install Letsencrypt SSL for your environment.

       i)Open the addon section in front of the node.

        

       ii) While installing SSL add your External Domain(Custom domains) into SSL.

       

3)Add your custom domain into the setting.

4)Now take web ssh of your Nginx Load balancer and go to
/etc/nginx/conf.d/ and open file ssl.conf

OR

go to configuration manager and open the file

/etc/nginx/conf.d/cache.conf/ssl.conf

 

Now add the below code in the server block

=====================================================================

server {
           listen 80;
           server_name mytestdomain.com;
          return 301 https://$server_name$request_uri;
}

 

This is a configuration code for an NGINX server block. It is directing any incoming requests on port 80 (HTTP) for the domain "mytestdomain.com" to be redirected to HTTPS.

Here is a breakdown of the code:

  1. server { }: This defines a server block that specifies the configuration for a particular server.

  2. listen 80;: This specifies that the server should listen for incoming requests on port 80 (HTTP).

  3. server_name mytestdomain.com;: This specifies the domain name that this server block should respond to. In this case, it is "mytestdomain.com".

  4. return 301 https://$server_name$request_uri;: This directive tells NGINX to return a 301 status code (permanent redirect) and redirect the request to the HTTPS version of the same domain. The $server_name and $request_uri variables are used to construct the URL of the redirected page.

In summary, this code ensures that any requests made to the HTTP version of the "mytestdomain.com" domain are automatically redirected to the HTTPS version of the same domain, which is more secure.

========================================================================

 


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 1546