Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using elastic beanstalk with ngnix proxy server. my application code was on node.js express framework. I am trying to access client ip via following code

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. I think it will be the proxy server's ip address.

How to access real client address from my application???

like image 873
ARUNBALAN NV Avatar asked Sep 27 '16 05:09

ARUNBALAN NV


People also ask

How do I get IP client Nodejs?

The easiest way to find your IP address in Node. js is to pull the client IP address from the incoming HTTP request object. If you are running Express in your Node app, this is very easy to do. Access the socket field of the Express request object, and then look up the remoteAddress property of that field.

Does Elastic Beanstalk have static IP?

Elastic Beanstalk routes outbound traffic through the NAT gateway. You can identify the source of the outbound traffic from the backend instances by the Elastic IP address. The Elastic IP address is a static IP address required by the NAT gateway.


1 Answers

I defer with @austince, the Client IP will be the first entry in the list for Elastic Beanstalk.

Example:

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));
like image 190
Adarsh Madrecha Avatar answered Sep 29 '22 08:09

Adarsh Madrecha