获取客户端用户真实 IP 地址 朴灿烈づ我的快乐病毒、 2022-01-17 04:33 361阅读 0赞 [为什么80%的码农都做不了架构师?>>> ][80_] ![hot3.png][] 现在需要获取请求到服务器的客户端 IP,一般有这些方式。 * remote\_addr:TCP 底层的会话 IP 地址,socket 连接的 IP 地址,不可伪造。但经过 Nginx 代理时会重置为 Nginx 的 IP,因为 Nginx 重新发起了和服务器的 TCP 连接。 * X-Real-IP:Nginx 中可以配置,将上一级的 remote\_addr 设置为 X-Real-IP。 * X-Forwarded-For:记录完整的代理链路,可以伪造。 可以看出为了兼容有 Nginx 代理的场景,使用 X-Forwarded-For 方式是做好的。这种方式也需要在 Nginx 中进行配置。 ## Nginx 配置 ## proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ## 参考 ## [从限流谈到伪造 IP][IP] [获取用户 IP 指引][IP 1] [获取用户 IP 的正确姿势][IP 2] [Nginx 四层、七层负载均衡的区别][Nginx] [ip-transparency-direct-server-return-nginx-plus-transparent-proxy][] 转载于:https://my.oschina.net/lvyi/blog/3045507 [80_]: https://my.oschina.net/u/2663968/blog/3051541 [hot3.png]: /images/20220117/8dd7453b94de4672a5fab6a4ac7d12fb.png [IP]: https://yonghaowu.github.io/2018/11/23/get_reql_ip/ [IP 1]: https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_5 [IP 2]: http://xxlegend.com/2016/11/01/%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7IP%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/ [Nginx]: https://www.jianshu.com/p/1308c7e0f425 [ip-transparency-direct-server-return-nginx-plus-transparent-proxy]: https://www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/
还没有评论,来说两句吧...