Search code examples
amazon-web-serviceslocaleload-balancing

Determine IP Address of Client Behind Amazon ELB


We have some PHP servers on EC2 behind ELB. We would like to determine the locale/region by IP address of the clients connecting to our servers. The problem is the PHP servers only see the IP address of the ELB. We would like to see the IP addresses of the clients passed through the ELB.


Solution

  • According to the AWS docs, the ELB should be setting the 'X-Forwarded-For' HTTP header which preserves the original client ip address:

    The X-Forwarded-For request header helps you identify the IP address of a client. Because load balancers intercept traffic between clients and servers, your server access logs contain only the IP address of the load balancer. To see the IP address of the client, use the X-Forwarded-For request header.

    You can access it using the following PHP code (assuming apache):

    $http_headers = apache_request_headers(); 
    $original_ip = $http_headers["X-Forwarded-For"];