WordPress wrong comment ip

WordPress wrong comment IP

Having trouble with WordPress wrong comment IP behind a Loadbalancing environment or especially Varnish / HAProxy?
Getting local IP address in comment posts e.g.?

Solutions is here!

 

If you have properly set your X-Forwarded-For field into your Header or don’t even know that this option exist 😉 you have also add this to your applications running behind such setup.

First solution: To get rid with it on WordPress all you have to do is a little PHP magic by rewriting your remote_addr variable with your X-Forwarded-For one.

Fire up your preferred text editor and add this to your wp-config.php file:

$behindproxy = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $behindproxy[0];

 

The second solution: If you are using Nginx as Webserver you can take advantage of the Module called HttpRealipModule by adding this to your nginx.conf http section:

http {
set_real_ip_from 192.168.1.2;#Replace with Varnish/HAProxy Server IP
real_ip_header X-Forwarded-For;
....

Bear in mind that you have to set the correct Varnish/HAProxy Server IP. It’s very important! Else it won’t resolve correct.

That’s usually all you have to do if your HTTP header is delivering the x-forwarded-for field.

Enjoy it!

HAProxy X-Forward-forVarnish x-forward-forwordpressWordpress local ipwordpress x-forward-forX-Forward-for