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

  • Dynamic mount point for Shared Volume Containers
  • docker set iptables options in docker-compose.yml
  • ceph rbd with docker volumes
  • Can't ping Docker containers via hostnames/names
  • Can't find equivalent of 'docker node ps' in Docker Python SDK
  • What is the correct way to deploy EAR on Weblogic with Docker
    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:
    

  • Docker Store Vs Docker Hub
  • Why are Docker container images so large?
  • Host-based-containers vs image-based-containers
  • Why is Docker not adding files to my container?
  • How do you get a file from a windows host to linux docker container?
  • Copy Docker container from boot2docker under OS X
  • 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.