Tuesday, 16 June 2015

AWS drupal issue: nginx+php missing client IP address when using reverse proxy

Symptoms of this issue

The most front end presentation for this issue is that you will get an error of "the IP has been blocked because of log-in  failure for too many times".
The source of this issue can be found when you print the result of the function called ip_address() in drupal, cause the only IP address you can get from drupal is 127.0.0.1.

Reason of this issue

At default, drupal gets the IP address from the PHP variable called $_SERVER['REMOTE_ADDR'], but this variable is set to 127.0.0.1 by nginx proxy server. However, a new variable named $_SERVER['HTTP_X_FORWARDED_FOR'] is set as the real IP by nginx reverse proxy if nginx realip module  is installed. And drupal also get the ability to detect this variable by doing some configuration.

Solution of this issue

Go to drupal root folder and open the file named sites/default/settings.php.
Go to the line 404 and you will find the following php code of  $conf['reverse_proxy'] = TRUE; which is commented. What we need to do is to un-comment it.
Jump to a few lines after, the variable definition of $conf['reverse_proxy_addresses'] will be found. We can just add '127.0.0.1' in the array which will be eliminated from client IP addresses.
After the previous configuration, the correct IP address should be received by drupal.


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete