2017-03-21 37 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; 
相關問題