I run a nginx reverse proxy server in the docker container. And the backend is a apache server running on the host, which is listening 10082
port. Laravel
handle the request. And I use
$request->getClientIp()
to get real ip. However, the result of visiting the server by http://myip:10082
directly without proxy conflicts with the result of visiting it by reverse proxy.
The test code in laravel:
echo $request->ip().'<br>';
echo $request->headers->get('X-Real-IP').'<br>';
echo $request->getClientIp().'<br>';
The result with proxy:
192.168.80.2
218.205.17.167
192.168.80.2
The result without proxy(the XX.XXX.236.29 is my real ip):
XX.XXX.236.29
XX.XXX.236.29
The configuration of nginx:
server {
listen 80;
server_name myserver.com;
access_log logs/myserver.access.log main;
location / {
proxy_pass http://myip:10082;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm so confused. Could someone help me solve it. Thanks!