【工具方法】Node获取用户ip
 发布于 12 年前  作者 yaochun  5042 次预览  最后一次回复是 12 年前  来自  

直接上代码:

var ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
5 回复
jiyinyiyong

适合上 Wiki, 有时间的话应该开始码 Wiki 了

fyjx2004

我怎么获取到的都是服务端的IP,是不是跟用了nginx反向有关?

bsspirit

.replace(’:remote-addr’, req.headers[‘x-forwarded-for’] || req.connection.remoteAddress)

我也遇到过类似的问题,nginx反向代理后,需要改一下配置 http://blog.fens.me/nodejs-nginx-log4js/

fyjx2004

@bsspirit 谢谢,,搞定了