简体   繁体   English

如何从部署在 AWS Elastic beanstalk 上的 node.js express 应用程序获取客户端 ip?

[英]How to get client ip from node.js express application deployed on AWS Elastic beanstalk?

I am using elastic beanstalk with ngnix proxy server.我正在将弹性 beantalkngnix代理服务器一起使用。 my application code was on node.js express framework.我的应用程序代码在 node.js express框架上。 I am trying to access client ip via following code我正在尝试通过以下代码访问客户端 IP

 var ip = event.headers['x-forwarded-for'] || event.connection.remoteAddress || event.socket.remoteAddress || event.connection.socket.remoteAddress;

but i always getting same client ip for all incoming request.但我总是为所有传入请求获得相同的客户端 IP。 I think it will be the proxy server's ip address.我认为这将是代理服务器的 IP 地址。

How to access real client address from my application???如何从我的应用程序访问真实的客户端地址???

I defer with @austince, the Client IP will be the first entry in the list for Elastic Beanstalk.我推迟了@austince,客户端 IP 将是 Elastic Beanstalk 列表中的第一个条目。

Example :示例

X-Forwarded-For: 182.12.12.123, 78.13.13.123 X-Forwarded-For: 182.12.12.123, 78.13.13.123

In case anyone, come looking for example code, here is what I have used in my project.万一有人来寻找示例代码,这是我在我的项目中使用的。

const _ = require('lodash');
const ipAddress = _.split(req.header('X-Forwarded-For'), ',');
ipadd = _.trim(_.first(ipAddress));

Here's the guide from AWS: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html#x-forwarded-for这是来自 AWS 的指南: https : //docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html#x-forwarded-for

X-Forwarded-For works, but is sometimes a list of IPs that have been hit along the way. X-Forwarded-For有效,但有时一路上被击中的 IP 列表 The Client IP should be the first entry in the list for Elastic Beanstalk.客户端 IP 应该是 Elastic Beanstalk 列表中的第一个条目

Ex :例如

X-Forwarded-For: client-ip-address, ip-address-1, ip-address-2 X-Forwarded-For:client-ip-address、ip-address-1、ip-address-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将node.js应用程序部署到弹性beantalk(使用express) - Deploy node.js application to elastic beanstalk (using express) Node.js应用程序未在AWS Elastic Beanstalk上运行 - Node.js application not running on AWS Elastic Beanstalk 如何在 Elastic Beanstalk AWS 上使用 MySQL 托管节点 js / express 服务器 - How to host a node js / express server with MySQL on Elastic Beanstalk AWS Node.js部署-AWS Elastic Beanstalk - Node.js deploy - AWS Elastic Beanstalk 如何将node.js应用程序部署到AWS Elastic Beanstalk中 - How to deploy a node.js app into AWS Elastic Beanstalk 如何使用AWS Elastic Beanstalk在Node.js上永远运行 - How to run forever on Node.js with AWS Elastic Beanstalk 如何在AWS ec2或Elastic Beanstalk上使用MongoDb部署我的Node.js应用程序? - How to deploy my Node.js Application with MongoDb on AWS ec2 or Elastic Beanstalk? 如何从Elastic Beanstalk Docker应用程序将node.js日志发送到Cloudwatch日志? - How to send node.js logs to Cloudwatch Logs from Elastic Beanstalk Docker application? 如何在HAProxy后面部署node.js表达应用程序? - How can a node.js express application be deployed behind HAProxy? 用于Python的Node.js Dockerfile部署到AWS Elastic Beanstalk - Dockerfile for Node.js with Python deploying to AWS Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM