gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:24:27 24 4
gpt4 key购买 nike

问题是我的以下站点结构中未设置PHP字段$_SERVER['HTTP_CLIENT_IP'],我该如何纠正呢?

Docker网桥网络上(位于cloudflare之后的主机上)的静态IP容器,名为“back”:

  • 在容器中运行的NGINX(172.24.02)
  • 容器中的Apache + PHP(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']);

    ?>

    运行上面的结果:
    HTTP_CLIENT_IP: 
    HTTP_X_FORWARDE‌​D_FOR:
    REMOTE_ADDR: 172.24.0.2
    HTTP_X_FORWARDED:
    HTTP_FORWARDED:
    HTTP_FORWARDED_FOR:

    最佳答案

    感谢@Devon提供打印$ _SERVER变量的建议,事实证明以下字段中包含客户端IP:

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

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

    关于php - 如何阻止客户端IP地址在Cloudflare/Docker/NGINX/LAMP站点中丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080910/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com