nginx 反代时如何传递客户端 ip

2017-08-05 23:59:55 +08:00
 wico97

看了网上大部分教程使用 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; 来传递客户端 ip 给目标站,如果人家检测 req.connection.remoteAddress 就获取了代理 ip。有办法这个把客户端 ip 传递给这个参数吗? 请教传递客户端 ip 的优雅方法

4269 次点击
所在节点    问与答
8 条回复
shyling
2017-08-06 00:22:45 +08:00
所以你需要用个上层的框架 trust proxy 后就会自动处理
wico97
2017-08-06 00:36:12 +08:00
@shyling 没明白,我修改不了源站呀。我只能修改自己代理站。貌似人家通过$_SERVER["REMOTE_ADDR"]来检测 ip 的
just1
2017-08-06 00:36:18 +08:00
remote_addr 是无法伪造的,x-real-ip 已经是很优雅的方法
shyling
2017-08-06 00:40:09 +08:00
哦,那你没办法了。我还以为你反代的自己的程序
msg7086
2017-08-06 13:42:07 +08:00
可以是可以的,就是很麻烦。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html

proxy_bind address [transparent]

The transparent parameter (1.11.0) allows outgoing connections to a proxied server originate from a non-local IP address, for example, from a real IP address of a client:

proxy_bind $remote_addr transparent;

In order for this parameter to work, it is necessary to run nginx worker processes with the superuser privileges and configure kernel routing table to intercept network traffic from the proxied server.

@just1
just1
2017-08-06 16:22:24 +08:00
@msg7086 https://www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/ 这个是原理
楼主的场景下源站不可控,是无法实现的
wico97
2017-08-06 17:34:05 +08:00
@just1
@msg7086 谢谢。知道还有 IP transparency 这个东西。
msg7086
2017-08-07 03:20:59 +08:00
@just1
@wico97
嗯服务器不在自己手里的话是不可能做到的。否则就能随意伪造来源请求了,想想也知道不可能啊。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/380734

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX