【问题标题】:How do I use the PROXY protocol to get the client's real IP address?如何使用 PROXY 协议获取客户端的真实 IP 地址?
【发布时间】:2013-08-01 15:51:57
【问题描述】:

AWS just added support to ELB 用于PROXY protocol,它包装 TCP 流并添加客户端 IP 地址(如代理所见),以便后端服务器可以访问客户端的 IP(否则它只会看到 ELB 的 IP )。

我知道 ELB 可以在 HTTP(S) 模式下运行,其中 ELB 插入一个 X-Forwarded-For 标头,但我在 TCP 模式下运行我的 ELB,以便我可以通过 SPDY 为我的站点提供服务。

如何修改我的 node.js 应用程序(使用 Express)以使用 PROXY 协议?

【问题讨论】:

    标签: node.js proxy


    【解决方案1】:

    I made a module caled proxywrap 包装 node.js Servers 并自动从连接流中剥离 PROXY 协议标头,并将 socket.remoteAddress 和 socket.remotePort 重置为 PROXY 标头中的值。

    它与内置的Server 模块(如http、https 和net)一起使用,作为模块的直接替代品:

    var http = require('http')
        , proxiedHttp = require('proxywrap').proxy(http)
        , express = require('express')
        , app = express()
        , srv = proxiedHttp.createServer(app); // instead of http.createServer(app)
    
    app.get('/', function(req, res) {
        res.send('IP = ' + req.connection.remoteAddress + ':' + req.connection.remotePort);
    });
    
    srv.listen(80);
    

    它也适用于spdy module:

    var proxiedSpdy = require('proxywrap').proxy(require('spdy').server);
    

    当然,您必须enable the PROXY protocol on your ELB(或您的应用所支持的任何代理)。

    【讨论】:

    • 是的。这在低水平上起作用;它不在乎 PROXY 标头之后会发生什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 2011-09-02
    • 2013-08-18
    • 2021-06-16
    • 2021-07-05
    • 2012-03-14
    • 2012-02-16
    相关资源
    最近更新 更多