Cloud / AWS /

Logging the correct IP in apache when using an Elastic Load

Cameron Maxwell

01 October 2012

1 Minute Read

A common problem when using ELB, or any proxy for that matter, is that the source IP for all traffic is the load balancer itself.

AWS ELB support the X-Forwarded-For protocol. This means that the original IP is now sent in an http header to your application.

This still requires you to change your app to look at this header instead of an environment variable.

For example, in PHP you would need to use a code snippet like this: ` $headers = apache_request_headers(); if (array_key_exists(‘X-Forwarded-For’, $headers)){ $hostname=$headers['X-Forwarded-For'] . ‘ via ‘ . $_SERVER["REMOTE_ADDR"]; } else { $hostname=$_SERVER["REMOTE_ADDR"]; } ` That still has it’s own issues in that you need to actually change the code if you have access to it.

One solution I am testing now is mod_cloudflare. This is based on mod_remoteip, but doesn’t break mod_rewrite.

More Tech Posts