如何使用 Restify 从请求对象中获取客户端 IP?

How can I get the client IP from a request object with Restify?

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

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

这有效:

req.connection.remoteAddress

其他答案在代理后不起作用,在这些情况下您将获得代理服务器地址。

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

如果代理在 x-forwarded-for header 中设置原始 IP,将在代理后面工作,许多人默认这样做,您可以很容易地添加到 nginx 之类的东西。