美文网首页
Docker && hyperf3.0 获取客户端的真实 IP

Docker && hyperf3.0 获取客户端的真实 IP

作者: geeooooz | 来源:发表于2023-07-09 16:12 被阅读0次

    如果您使用nginx代理,

    首先添加代理配置

      location / {
             # proxy host and ip information
             proxy_set_header Host $http_host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
             # proxy
             proxy_pass http://xx.xx.xx.xx:xxxx;
         }
    

    然后您可以通过请求 header 中的【X-Real-IP】值来生成真实IP。

    这样应该也可以:

    location ^~ /lgbhf/ {
                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        Upgrade $http_upgrade;
                proxy_set_header        Connection "upgrade";
                proxy_pass http://127.0.0.1:9501/lgbhf/;
        }
    

    代码中这么写:

    $headers = array_map(function($value) {
                return $value[0];
            }, $this->request->getHeaders());//获取headers信息
    
    // 获取客户端的真实 IP
    $clientIp = $headers['x-real-ip'] ?? '';
    

    相关文章

      网友评论

          本文标题:Docker && hyperf3.0 获取客户端的真实 IP

          本文链接:https://www.haomeiwen.com/subject/jfysudtx.html