To make Drupal detect the IP of the client correctly you will have to update $_SERVER['HTTP_X_FORWARDED_FOR']
by prefixing the true client IP to it or setting it to the true client IP itself.
$_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_REAL_IP'] . ', ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
or
$_SERVER['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_REAL_IP'];
The challenge is that by the time settings.php is parsed, the Request class has already been instantiated and the server variables have already been processed and loaded into the request object. So doing this in settings.php will not help solve the problem.
In index.php
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
settings.php is processed in $kernel->handle
.
By then $request
has already been loaded with values from $_SERVER
So to make Drupal detect the correct IP you will have to patch index.php to add the below line before $request
is initialized.
There is likely a PHP OOPs / Symfony approach by overriding some class / extending some class to fix this more appropriately but this will solve the problem.