问题
How can I make sure that the client IP address is forwarded by ngrok?
My test code keeps insisting that all the requests are coming from 127.0.0.1 because of ngrok but i want to log the actual client IP instead. Load balancers usually set a header in X-Forwarded-For or x-real-ip but I'm not sure what the process for ngrok is ...
console.log('req.headers[\'x-real-ip\']', req.headers['x-real-ip']);
console.log('req.headers[\'X-Forwarded-For\']', req.headers['X-Forwarded-For']);
console.log('req.ip', req.ip );
console.log('req.connection.remoteAddress', req.connection.remoteAddress);
console.log('req.connection.remoteAddress', req.connection.remoteAddress);
console.log('req.socket.remoteAddress', (req.socket && req.socket.remoteAddress));
console.log('req.socket.socket.remoteAddress', (req.socket.socket && req.socket.socket.remoteAddress));
Everything either prints undefined
or 127.0.0.1
so far. Which means I need to configure ngrok somehow, I think.
回答1:
Unfortunately, all header names in Node.JS are lowercase. Use
req.headers['x-forwarded-for']
instead of
req.headers['X-Forwarded-For']
来源:https://stackoverflow.com/questions/37668942/how-to-get-the-real-client-ip-for-a-request-when-tunneling-with-ngrok