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

  • Expose Docker Ports and Access with DNS for easier Ansible-Access
  • Docker in debugging mode
  • MobileFirst Container CLI Extensions are not installed
  • Unable to create machine in docker
  • How to gracefully shut down a server running in docker
  • How to list images and tags from the gcr.io Docker Registry using the HTTP API?
  • 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!

  • docker run <image> : /usr/bin/env: ruby.exe : No such file or directory
  • How to run cucumber/selenium tests in Docker?
  • Can't install agent for Cassandra in Docker container
  • What is the best way to use different environments with Docker Swarm?
  • Connecting to a running Erlang application release in a docker container
  • Docker eating up disk space /var/lib/docker/devicemapper/devicemapper
  • 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.