There are a lot of places, where you need client's IP address in Drupal (or any other CMS/web app of course). The problem arises, when you use a reverse proxy server (like Varnish), since every request to web server will be done by the latter. We will have every single visitor of our website coming from a single IP (reverse proxy), as a result.
Drupal is smart enough to overcome that. Reverse proxy servers can be configured to forward original client IP in a request header (usually X-Forwarded-For). This value can be used on web server to know where our visitory come from. In Drupal we have function ip_address(), which will read and return client's IP. If we take a look at this function's code, we can see that it already has support for situations, where reverse proxy is used. This function will still return Varnish's IP address by default, though.
To make things working as we expected, we have to configure at least two variables in settings.php:
// Tell Drupal that we are behind a reverse proxy server $conf['reverse_proxy'] = TRUE; // List of trusted IPs (IP numbers of our reverse proxies) $conf['reverse_proxy_addresses'] = array( '127.0.0.1', );
That's it. Now ip_address() returns IP number that was sent via request header. There is also a possibility to use custom header name:
// Drupal will look for IP in $_SERVER['HTTP_MY_CUSTOM_HEADER'] $conf['reverse_proxy_header'] = 'HTTP_MY_CUSTOM_HEADER';