Nginx proxy pass real ip doesn't work

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()

  • Installed gems not found by bundler when BUNDLE_PATH changed with Docker
  • Kubernetes access labels from containers
  • RabbitMQ cluster by docker-compose on different hosts and different projects
  • How to pass docker arguments during docker run command or docker-compose up command
  • docker run local script without host volumes
  • How to implement my project with docker and git
  • 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!

  • Why is my dockerfile not copying directories
  • Is there a Docker Swarm dashboard like Kubernetes' dashboard/ui? [closed]
  • How to 'avahi-browse' from a docker container?
  • second ssh session can not connect to docker host
  • “Not a tty” error in Alpine-based duplicity image
  • How to use docker and pycharm on mac?
  • One Solution collect form web for “Nginx proxy pass real ip doesn't work”

    Since you have set the X-Real-IP header in your nginx proxy, it’s the real ip.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.