How to stop the client IP address getting lost in a Cloudflare/Docker/NGINX/LAMP site?

The problem is the PHP field $_SERVER['HTTP_CLIENT_IP'] is not being set in my following site structure, how should I correct this?

Static IP containers on a Docker bridge network (on a host behind cloudflare) named ‘back’:

  • Vagrant and / or Docker workflow with full OS X filesystem integration for seamless local feel?
  • Convert “docker run …” into “docker create …” && “docker start …”
  • Migrating existing Docker containers to Docker Compose
  • Create Docker container with both Java and Node.js
  • Whenever gem 'failed to load command: rake'
  • Can't clone public repo from within Dockerfile
    1. NGINX running in container (172.24.02)
    2. Apache + PHP in container (172.24.0.3)

    nginx.conf:

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://172.24.0.3;
    }
    

    test.php

    <?php
    
    echo('HTTP_CLIENT_IP: '.$_SERVER['HTTP_CLIENT_IP']);
    echo('<br>HTTP_X_FORWARDE‌​D_FOR'.$_SERVER['HTTP_X_FORWARDE‌​D_FOR']);
    echo('<br>REMOTE_ADDR'.$_SERVER['REMOTE_ADDR']);
    echo('<br>HTTP_X_FORWARDED'.$_SERVER['HTTP_X_FORWARDED']);
    echo('<br>HTTP_FORWARDED'.$_SERVER['HTTP_FORWARDED']);
    echo('<br>HTTP_FORWARDED_FOR'.$_SERVER['HTTP_FORWARDED_FOR']);
    
    ?>
    

    Running the above yields:

    HTTP_CLIENT_IP: 
    HTTP_X_FORWARDE‌​D_FOR: 
    REMOTE_ADDR: 172.24.0.2
    HTTP_X_FORWARDED: 
    HTTP_FORWARDED: 
    HTTP_FORWARDED_FOR:
    

  • sharing of OS image resources across multiple docker containers
  • Getting “connection refused” when trying to access etcd from within a Docker container
  • Pod “mysql” is forbidden: no API token found for service account default/default
  • automatic log rotation is not executing
  • Docker: Can't pick up config file for nginx on Windows 10
  • how can I open a flask app on my browser that's running remotely on docker?
  • One Solution collect form web for “How to stop the client IP address getting lost in a Cloudflare/Docker/NGINX/LAMP site?”

    Thanks to @Devon for making the suggestion of printing out the $_SERVER variable, turns out the following fields had the client IP in them:

    $_SERVER['HTTP_CF_CONNECTING_IP']
    $_SERVER['HTTP_X_FORWARDED_FOR']
    

    The first gave the client IP, the second gave client IP, cloudflare IP

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