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’:

  • Unable to push a plugin using docker plugin push command
  • docker mounts host's / directory to container's / in debian wheezy 7.8
  • Orion sending error message “connection failed, after 100 retries”
  • What components should be “containerized” - Docker
  • How to define multiple tasks in elastic beanstalk docker json
  • How is it possible for two identical Docker images to have different packages installed?
    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:
    

  • Implementing Simulation application using AKKA
  • Docker multi host connection issue
  • Cron isn't running when I start my docker container
  • docker command line list arguments
  • Docker volume, change file permissions/owner
  • How to keep track of pip installed packages on one docker container to use them in another
  • 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.