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

 

Leave a Reply