微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 具有亚马逊ELB后面的node-js的远程IP地址

我在弹性负载平衡器(elb)后面的实例存储亚马逊机器上有一个节点应用程序.然而,远程IP地址似乎总是一样的.我使用这段代码获取客户端的IP地址(通过connect / express):

req.socket.remoteAddress

我没有从节点文档中得到任何其他东西.任何提示

解决方法

这是一个解决方案,如果你使用快递:
根据 documentation,您可以为您的快递实例启用信任代理,然后req.ip将填充正确的IP地址.

By enabling the “trust proxy” setting via app.enable(‘trust proxy’),
Express will have kNowledge that it’s sitting behind a proxy and that
the X-Forwarded-* header fields may be trusted,which otherwise may be
easily spoofed.

Enabling this setting has several subtle effects. The first of which
is that X-Forwarded-Proto may be set by the reverse proxy to tell the
app that it is https or simply http. This value is reflected by
req.protocol.

The second change this makes is the req.ip and req.ips values will be
populated with X-Forwarded-For’s list of addresses.

以下是一个例子:

var app = express();
app.enable('trust proxy');
// ...

app.use(function(req,res,next) {
  console.log('client ip address:',req.ip);
  return next();
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐