I've apache开发者_Go百科2 and nginx. I set "trust proxy headers" to true in configuration, but anyway get internal ip when calls $request->getClientIp();
What do I wrong?
If I calling getClientIp
with parameter $proxy = true
then I getting correct IP. But there is configuration where proxy headers enabled, aren't that enough?
Actually this is an issue, fixed by this merge https://github.com/symfony/symfony/commit/40599ec0a24e688ef5903e2bd3cfb29b5ab29a18
In short: you always need to use $proxy = true
if you're planning on using some kind of reverse proxy. With this parameter set (and trustProxyData();
enabled), $this->getClientIp();
will return the correct IP with reverse proxy.
Explanation: even after configured, proxy headers will return HTTP_X_FORWARDED_FOR
or HTTP_CLIENT_IP
as the user IP, while REMOTE_ADDR
will return server localhost address (most likely 127.0.0.1). $proxy = true
checks exactly that. Here's the source code for this function:
public function getClientIp($proxy = false)
{
if ($proxy) {
if ($this->server->has('HTTP_CLIENT_IP')) {
return $this->server->get('HTTP_CLIENT_IP');
} elseif (self::$trustProxy && $this->server->has('HTTP_X_FORWARDED_FOR')) {
return $this->server->get('HTTP_X_FORWARDED_FOR');
}
}
return $this->server->get('REMOTE_ADDR');
}
精彩评论