如何使用PROXY协议获取客户端的真实IP地址? [英] How do I use the PROXY protocol to get the client's real IP address?

查看:799
本文介绍了如何使用PROXY协议获取客户端的真实IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS刚刚向ELB添加了支持用于 PROXY协议,该协议包装TCP流并添加客户端IP地址(如代理所示),以便后端服务器可以访问客户端的IP(否则它将只看到ELB的IP).

AWS just added support to ELB for the PROXY protocol, which wraps TCP streams and adds the client IP address (as seen by the proxy) so that the backend server has access to the client's IP (since it would otherwise just see the ELB's IP).

我知道ELB可以在HTTP(S)模式下运行,其中ELB插入X-Forwarded-For标头,但是我以TCP模式运行ELB,以便可以通过SPDY .

I know that ELB can run in HTTP(S) mode, where the ELB inserts a X-Forwarded-For header, but I run my ELBs in TCP mode so that I can serve my site over SPDY.

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

How can I modify my node.js app (using Express) to use the PROXY protocol?

推荐答案

我制作了一个封装了节点的模块代理proxywrap .js Server s并自动从连接的流中剥离PROXY协议标头,并将socket.remoteAddresssocket.remotePort重置为PROXY标头中找到的值.

I made a module caled proxywrap that wraps node.js Servers and automatically strips the PROXY protocol headers from connections' streams, and resets socket.remoteAddress and socket.remotePort to the values found in the PROXY headers.

它可以与内置的Server模块(例如httphttpsnet)一起使用,以代替模块:

It works with the built-in Server modules (like http, https, and net) as a drop-in replacement for the module:

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模块一起使用:

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

当然,您必须在ELB上启用PROXY协议(或您的应用支持的任何代理).

Of course, you'll have to enable the PROXY protocol on your ELB (or whatever proxy your app is behind).

这篇关于如何使用PROXY协议获取客户端的真实IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆