Get Remote Host Client IP Behind Load Balancers (ELB)

I was working on “Private AWS Cloudfront Distribution” for AWS Elastic Load Balancer (ELB), allowing our application servers private access to our AWS cloudfront.

But PHP Server Environment Variable $_SERVER[“REMOTE_ADDR”] is displaying suspicious results, it really tedious to get remote host client IP behind ELB 🙁

Now we have 2 options…

1. mod_rpaf and Amazon ELB
http://tech.superhappykittymeow.com/?p=281
http://giantdorks.org/alain/easily-get-the-correct-client-ip-with-mod_rpaf/

if ($_SERVER[“HTTP_X_FORWARDED_FOR”]) {
$realclientip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
} else {
$realclientip = $_SERVER[“REMOTE_ADDR”];
}

2. %{X-Forwarded-For}i from Apache Log (Time Consuming)
http://blog.kenweiner.com/2009/09/amazon-elb-capturing-client-ip-address.html
https://forums.aws.amazon.com/message.jspa?messageID=216977

You Apache log format would then look something like this:
LogFormat “\”%{X-Forwarded-For}i\” %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-agent}i\”” combined-elb
CustomLog log/acces_log combined-elb

Apache Module mod_remoteip

PHP: $_SERVER[“HTTP_X_FORWARDED_FOR”]
PHP: $_SERVER[“HTTP_X_CLUSTER_CLIENT_IP”]

$_SERVER[‘REMOTE_ADDR’]; Apache Enviromental Variable is not working on your server 🙂
http://php.net/manual/en/function.getenv.php
http://www.zytrax.com/tech/web/env_var.htm