如何从Restify的请求对象获取客户端IP?

时间:2015-01-26 20:38:45

标签: node.js restify

我很难找到如何从路由访问REST客户端的IP地址。

server.get('api/foo', function(req, res, next) {
    // How can I access the IP address of the requester from here?
}

2 个答案:

答案 0 :(得分:16)

这有效:

req.connection.remoteAddress

答案 1 :(得分:11)

其他答案不会在代理后面工作,在这种情况下您将获得代理服务器地址。

req.headers['x-forwarded-for'] || req.connection.remoteAddress;

如果代理在x-forwarded-for标头中设置原始IP,默认情况下可以添加到nginx之类的内容,则可以在代理后面工作。