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

  • Best way to extend Dockerfile to pull deps/env vars from common/shared repositories
  • Traefik accessLog configuration
  • How to get docker volumes from newly built image?
  • Adjust OOM killer for subprocess in Docker container
  • Make pdb work with docker
  • How do you get a file from a windows host to linux docker container?
    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:
    

  • how can docker containers have a different flavor than the host
  • Docker Pull taking unexpectedly long time via Ansible
  • Docker Containers with different Network Interface
  • How to install docker without cgroup
  • Apache2 in docker container gives 403 on statically served files
  • error response from daemon: no such container: dionaea
  • 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.