How to capture and forward incoming IP address in Node-RED?

I have been using Node-RED for 5 years now and have decent exposure to it. We use it for various HTTP calls and use it as a relay from internet to out internal API servers.

We would like to capture the IP address of incoming requests, but once the calls hits Node-RED application, we only see Node-RED server's IP address in our internal API request headers.

I have tried to capture the msg.req.ip and msg.req.connection.remoteAddress as shown in this topic Http in node, caller IP address - #8 by Colin but that always returns 127.0.01 which I think is the local IP address of the Node-RED server as seen by the UI builder.

Can someone help me in capturing and may be forwarding the incoming IP address. Thanks.

Hi Pawanpillai,

is your router/firewall set up to proxy the requests or just forward them?

If its proxying them you probably wont see the original IP

Try msg.req.headers['x-forwarded-for'] or it could be msg.req.headers['x-forwarded-For'] see what you get.

Thanks, I tried both and both returned undefined.

msg.xff = msg.req.headers['x-forwarded-for'];
msg.xfF = msg.req.headers['x-forwarded-For'];

prints:
msg.xff: undefined
msg.xfF: undefined

It may not being forwarded.

Can you confirm its forwarded or proxied from your firewall?

My company's IT team manages the Node-RED server backend, I will check with them on Monday but as they use ngnix, its most probably setup as a proxy. I will keep you posted. Thank you for all the help so far.

You may need to set an appropriate value for the Express setting "trust proxy" e.g...

In Node-REDs settings.js file:

    httpServerOptions: {
        "trust proxy": true // take left-most entry in the X-Forwarded-* header.
    },

REF: Express 5.x - API Reference

Be sure to read up on the implications this means for your env. e.g from the docs:

Indicates the app is behind a front-facing proxy, and to use the X-Forwarded-* headers to determine the connection and the IP address of the client. NOTE: X-Forwarded-* headers are easily spoofed and the detected IP addresses are unreliable.