在nodejs应用程序中了解我的客户端IP

我有两层应用程序结构(Nginx(代理)+ nodejs(应用程序)。我需要在我的应用程序(nodejs)中获得我的客户端IP(谁访问我的网站)的结束。现在我的客户端IP,它正在login我的Nginx日志文件(通过启用下面的conf

proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

现在我在我的应用程序服务器中获取Nginx服务器IP,但是我不想让我的Nginx服务器IP,而不是我需要在我的应用程序中获取我的原始客户端IP

这是应用程序代码,我们正在使用它来获取应用程序服务器中的客户端IP。

 request.headers['x-forwarded-for'] || request.info.remoteAddress 

哪里; Nodejs–> Happijs框架

你应该使用下面的代码:

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

如果你使用Hapi框架,那么你应该使用下面的代码:

 var ip = request.headers['x-forwarded-for'] || request.info.remoteAddress; 

SOLUTION_SOURCE: 用nginx和节点获取ip用户 ,

您可以configurationNGINX使用以下设置传递客户端的IP地址:

 location / { proxy_pass https://fotogena.co:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # This line. proxy_connect_timeout 1000; proxy_send_timeout 1500; proxy_read_timeout 2000; 

}

然后,您可以使用来自req.headers [“X-Real-IP”]的HTTP标头X-Real-IP。