Skip to main content

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

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:

  1. $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?

  1. * Each element of this array is the IP address of any of your reverse
  2.  * proxies. Filling this array Drupal will trust the information stored
  3.  * in the X-Forwarded-For headers only if Remote IP address is one of
  4.  * these, that is the request reaches the web server from one of your
  5.  * reverse proxies. Otherwise, the client could directly connect to
  6.  * your web server spoofing the X-Forwarded-For headers.
  7.  */
  8.  $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:

  1. $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.