Having your WordPress site behind a Load Balancer is essential if you want to achieve High Availability (HA) for your site. But this could have some unwanted behaviours for your WP installation. One of those unwanted behaviour is not having the real IP address of your visitors in your logs or your comments.
This happens because your load balancers forwards the request to one of HTTP nodes in your cluster. Having for consequence that the IP of your Load Balancer is what appears when a user sends a comment. This can be annoying if you want to filter out SPAM comments for instance.
There is an easy fix for that. Adding the following code into your wp-config.php
file will override the default behaviour of the $_SERVER
variable with the HTTP_X_FORWARDED_FOR
header. And voilà, problem solved.
If it doesn’t solve the issue, check at your Load Balancer to make sure it is configured properly to send the header correctly.
// Use X-Forwarded-For HTTP Header to Get Visitor's Real IP Address
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$http_x_headers = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
$_SERVER['REMOTE_ADDR'] = $http_x_headers[0];
}
Very good article. I am going through many of these issues as well..
Thanks Mike, I’m glad you like it!
beautiful post
Thanks Dimitri
Thats worked, thanks
Glad you got it working.
Oh gawd!!
Will you marry me? 😀
Thank you! That fixed our problem
Haha, glad that could help you 😀
Have a great day!