V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wico97
V2EX  ›  问与答

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

  •  
  •   wico97 · 2017-08-05 23:59:55 +08:00 · 4264 次点击
    这是一个创建于 2452 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    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
        6
    just1  
       2017-08-06 16:22:24 +08:00 via Android
    @msg7086 https://www.nginx.com/blog/ip-transparency-direct-server-return-nginx-plus-transparent-proxy/ 这个是原理
    楼主的场景下源站不可控,是无法实现的
    wico97
        7
    wico97  
    OP
       2017-08-06 17:34:05 +08:00
    @just1
    @msg7086 谢谢。知道还有 IP transparency 这个东西。
    msg7086
        8
    msg7086  
       2017-08-07 03:20:59 +08:00
    @just1
    @wico97
    嗯服务器不在自己手里的话是不可能做到的。否则就能随意伪造来源请求了,想想也知道不可能啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3426 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:06 · PVG 20:06 · LAX 05:06 · JFK 08:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.