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

Cod*_*er1 9 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?
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*er1 18

这有效:

req.connection.remoteAddress


Kur*_*ler 16

其他答案在代理后面不起作用,在这些情况下你会获得代理服务器地址.

req.headers['x-forwarded-for'] || req.connection.remoteAddress;
Run Code Online (Sandbox Code Playgroud)

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