Redirect http to https on nginx behind load balancers

Date Posted:05-03-2015

This post explains on how to redirect all http request to https on nginx server which are behind load balancer.

Assumption:
  1. Ubuntu or Centos
  2. Nginx Webserver- If nginx is not installed on the serer, refer the following URL to install.
Implementation:

Let’s assume that the server has only one website and the configuration location is /etc/nginx/site-enabled/default.

Open the configuration and append the following section under server  blocks.

vi /etc/nginx/sites-enabled/default

if ($http_x_forwarded_proto != 'https') {
 rewrite ^(.*) https://$host$1 permanent;
}

Where,

$http_x_forwarded_proto – >This refer to origin request or client request

!=https -> If the request is Not Equal to https
rewrite -> Rewrite rule

https://$host$1 – Redirects the request to https

Verify the syntax of the nginx configuration.

nginx -t

Restart the service to make changes into effect.

/etc/init.d/nginx reload

 

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top