Symfony Request and reverse proxy

I have apache2 and nginx. I set the "trusted proxy headers" to true in the configuration, but in any case I get an internal ip when calls $request->getClientIp(); What am I wrong?

If I call getClientIp with the parameter $proxy = true , then I get the correct IP. But is there a configuration in which proxy headers are included is not enough?

+4
source share
2 answers
+1
source

In short: you always need to use $proxy = true if you plan to use some kind of reverse proxy. If this parameter is set (and trustProxyData(); enabled), $this->getClientIp(); will return the correct IP address with a reverse proxy server.

Explanation: even after configuration, proxy headers return HTTP_X_FORWARDED_FOR or HTTP_CLIENT_IP as the user's IP address, and REMOTE_ADDR returns the local server server address (most likely 127.0.0.1). $proxy = true checks for exactly this. Here is 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'); } 
0
source

All Articles