Redirecting EC2 Elastic Load Balancer from HTTP to HTTPS

Amit Badheka picture Amit Badheka · Jul 7, 2014 · Viewed 83.1k times · Source

I want to redirect all the HTTP request to https request on ELB. I have two EC2 instances. I am using nginx for the server. I have tried a rewriting the nginx conf files without any success. I would love some advice on it.

Answer

Dmitry Mukhin picture Dmitry Mukhin · Jul 7, 2014

ELB sets X-Forwarded-Proto header, you can use it to detect if original request was to HTTP and redirect to HTTPS then.

You can try this in your server conf:

if ($http_x_forwarded_proto = 'http') {
    return 301 https://yourdomain.com$request_uri;
}

Take a look at ELB docs.