Using Varnish or ngnix as a proxy will always return ip as 127.0.0.1
bainternet opened this issue · comments
Hi guys,
I have a few sites that the IP recorded for each activity is always 127.0.0.1 instead of the real user/guest IP.
It took me a while but I found that the one thing they have in common is that they were all served using a proxy either ngnix or varnish.
which makes sense because the server (apache or ngnix) gets the request from the proxy (varnish or ngnix) which is on the same machine and that is why i get the local host IP (127.0.0.1) as user IP which is actually wrong.
looking at the code this line in specific is the problematic one
https://github.com/KingYes/wordpress-aryo-activity-log/blob/master/classes/class-aal-api.php#L55
A better way to get the real IP even in the case of a proxy you should first look in the
$_SERVER['HTTP_CLIENT_IP']
var then in the $_SERVER['HTTP_X_FORWARDED_FOR']
and only then in the $_SERVER['REMOTE_ADDR']
for example:
function get_real_ip(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
return $_SERVER['HTTP_CLIENT_IP'];
}else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
thanks.
Happy Yom haatzmaut;
You right. I will fix this later. :)
Thanks
Thanks!