Force HTTPS when using AWS ELB and nginx

You only have to setup the SSL certificate on Amazon ELB instead of every EC2 instance that behind ELB, normally you only receive HTTP traffic from Amazon ELB that serves both HTTP and HTTPS endpoints. The problem is that forcing HTTPS will result in a redirect loop when nginx only listens 80 port. A solution is forwarding HTTPS requests to another port, like 1443, in Amazon ELB.

Amazon ELB

Listeners configurations:

HTTP 80 >> HTTP 80
HTTPS 443 >> HTTP 1443

and health check target port should be HTTP:1443 instead of HTTP:80 or HTTPS:443.

nginx

in /etc/nginx/sites-available/your_website.conf

server {
    listen 80;
    server_name yahoo.streetvoice.com;
    rewrite ^ https://$host$request_uri? permanent;
}

server {
    listen 1443;
    server_name yahoo.streetvoice.com;

    ...
}

ref:
http://serverfault.com/questions/619971/redirect-all-http-requests-behind-amazon-elb-to-https-without-using-if
http://scottwb.com/blog/2013/10/28/always-on-https-with-nginx-behind-an-elb/