Forcing HTTPS on AWS EC2 load-balancer cluster

Recently i had to force rewrite HTTP to HTTPS on servers running under an AWS EC2 load-balancer. My first instinct was to add rewrite rule like the one below.

But that did not work and after a bit of debugging i was able to figure out that it was happening because the cluster did not have any SSL certificate installed. They were still running on port 80.

What was happening was that the SSL certificate was installed on the load-balancer and the rewrite had to be done on that level. And since i did not have access to the load-balancer, i was stuck at this for a couple of hours before i debugged the server response and found that it had two extra server vars which indicated SSL traffic flowing from the load balancer to the pool machines.

HTTP_X_FORWARDED_PROTO contains “http” or “https” based on the type of traffic. HTTP_X_FORWARDED_PORT is the port for the traffic.

So, using above two vars i was able to detect and rewrite successfully. Following is the PHP code.

alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.
alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.