关于 Request::getClientIps () 获取真实 IP 的一些疑问

https://github.com/symfony/http-foundation...

不明白 $trustedProxies 这个变量的实际意义

起因是因为需要获取真实IP,但是发现 getClientIps() 总是返回的 REMOTE_ADDR ,我需要获取的是 HTTP_X_REAL_IP

代码走到这里不明白这个判断的意义,也没有找到设置可信任代理数组的位置

debug的时候使用 \Symfony\Component\HttpFoundation\ServerBag

$this->server->get('HTTP_X_REAL_IP') 或者 $this->server->get('HTTP_X_FORWARDED_FOR') 是可以获取到结果的

求解惑

本帖已被设为精华帖!
本帖由系统于 4年前 自动加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5
Summer

使用 setTrustedHeaderName() 做设置。

getClientIp 有个注释可以看看


/**
 * Returns the client IP address.
 *
 * This method can read the client IP address from the "X-Forwarded-For" header
 * when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
 * header value is a comma+space separated list of IP addresses, the left-most
 * being the original client, and each successive proxy that passed the request
 * adding the IP address where it received the request from.
 *
 * If your reverse proxy uses a different header name than "X-Forwarded-For",
 * ("Client-Ip" for instance), configure it via "setTrustedHeaderName()" with
 * the "client-ip" key.
 *
 * @return string The client IP address
 *
 * @see getClientIps()
 * @see http://en.wikipedia.org/wiki/X-Forwarded-For
 */
public function getClientIp()
7年前 评论

@Summer 解决了 :D
需要提前设置好可信任IP,因为当前机器上一层还有一个nginx,也属于内网的,需要把上一层的IP设置到信任列表里,才会继续获取到最外层的真实IP

具体方法:

Request::setTrustedProxies(['192.168.0.1'])

Request::getClientIp() // 获取真实IP

7年前 评论

@FreeDKR 你一个参数是如何实现的,查询setTrustedProxies方法后是两个参数

3年前 评论

@FreeDKR 这个和前后端分离也有关系,我的前端是跑在本地的,获取不到支持前端的服务器,但是我放到了服务器上使用 Request::setTrustedProxies(['192.168.0.1']) Request::getClientIp() // 获取真实 IP
使用这个是可以获取到IP的

3年前 评论

Laravel 5.8.*

request()->setTrustedProxies(request()->getClientIps(), \Illuminate\Http\Request::HEADER_X_FORWARDED_FOR);
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!