【工具方法】Node获取用户ip
发布于 11 年前 作者 yaochun 4396 次浏览 最后一次编辑是 8 年前

直接上代码:

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

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

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

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

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

@bsspirit 谢谢,,搞定了

回到顶部