如何在Cloudflare / Docker / NGINX / LAMP站点中阻止客户端IP地址丢失?

时间:2016-12-10 22:19:07

标签: php nginx docker cloudflare

问题是我的以下网站结构中没有设置PHP字段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; } ,我应该如何更正?

Docker网桥上的静态IP容器(位于cloudflare后面的主机上)名为' back':

  1. NGINX在容器中运行(172.24.02)
  2. 容器中的Apache + PHP(172.24.0.3)
  3. nginx.conf:

    <?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']);
    
    ?>
    

    test.php的

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

    运行上述产量:

    {{1}}

1 个答案:

答案 0 :(得分:2)

感谢@Devon提出打印$ _SERVER变量的建议,结果显示以下字段中包含客户端IP:

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

第一个给了client IP,第二个给了client IP, cloudflare IP