Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Varnish or ngnix as a proxy will always return ip as 127.0.0.1 #53

Closed
bainternet opened this issue May 5, 2014 · 2 comments
Closed
Assignees
Milestone

Comments

@bainternet
Copy link
Contributor

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;

@KingYes
Copy link
Member

KingYes commented May 5, 2014

You right. I will fix this later. :)
Thanks

@KingYes KingYes self-assigned this May 5, 2014
@KingYes KingYes added this to the 2.1.3 milestone May 5, 2014
@KingYes KingYes modified the milestones: 2.1.4, 2.1.3 Jun 20, 2014
@KingYes
Copy link
Member

KingYes commented Jun 22, 2014

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants