2017-03-21 40 views
1

我有两层应用程序结构(Nginx(proxy)+ nodejs(app)。我需要在我的应用程序中获取我的客户端IP(谁访问我的网站)的NodeJS)。现在,我的客户端IP,它越来越记录在我的Nginx的日志文件(通过启用下面的conf在nodejs应用程序中了解我的客户端ip

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框架

+1

我认为这是你正在寻找的答案: HTTPS: //stackoverflow.com/questions/30943112/get-ip-user-with-nginx-and-node#30943228 – Red8

回答

0

SOLUTION_SOURCE:get ip user with nginx and node

您可以配置NGINX与以下设置通过客户端的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。

1

下面的代码,您应该使用:

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

如果您正在使用哈皮框架,那么你应该在下面的代码中使用:

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