簡體   English   中英

如何從請求Node.js獲取IP

[英]How to get ip from request nodejs

我想從發布請求中獲取IP。

var ip = req.ip || req.headers['x-forwarded-for'] || 
req.connection.remoteAddress || 
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null)

res.json({
  success: true,
  message: 'ip'+ip
});

但它返回一個null值。 我在服務上使用(它不是本地主機)

有什么解決辦法嗎? 所有解返回null ;

如果它位於像Nginx這樣的反向代理之后,請嘗試使用ngx_http_core_module中的嵌入式變量。 (至少這就是我幾年來所做的事情)。

Nginx配置文件

....
location / {
   proxy_set_header X-Real-IP $remote_addr;
....

Node.js

req.headers["x-real-ip"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM