How to get the real client ip for a request when tunneling with ngrok

偶尔善良 提交于 2019-12-08 19:11:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!