Setup Drupal's Reverse Proxy Allowed Addresses Behind ELB on AWS

Profile picture for user Phil Frilling
By Phil Frilling, 20 August, 2014
Today I was trying to setup a site behind an Elastic Load Balancer (ELB) on Amazon's EC2. I was able to set the configuration array to on in settings.php:

$conf['reverse_proxy'] = TRUE;
However, ELB's within AWS are setup to auto scale, which in turn can make their IP addresses change. So, how can we setup our reverse_proxy_addresses configuration to use a dynamic address?

* Each element of this array is the IP address of any of your reverse
 * proxies. Filling this array Drupal will trust the information stored
 * in the X-Forwarded-For headers only if Remote IP address is one of
 * these, that is the request reaches the web server from one of your
 * reverse proxies. Otherwise, the client could directly connect to
 * your web server spoofing the X-Forwarded-For headers.
 */
 $conf['reverse_proxy_addresses'] = array('a.b.c.d', 'e.f.g.h');
I stumbled across Katrin's blog post and was able to make this work using the code below:

$conf['reverse_proxy_addresses'] = array_map('gethostbyname', array_map('gethostbyaddr', gethostbynamel($_SERVER['HTTP_HOST'])));
This code does a lookup on the names, and because the code is within Amazon's system, it finds the ELB's private IP addresses, and correctly maps the addresses. Likewise, if the site were to get a spike in traffic and the ELB would auto scale, the IP addresses would still map correctly, even though they would have changed.