Node.js:获取客户端的 IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19266329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js: Get client's IP
提问by Sam
req.connection.remoteAddress, req.headers['x-forwarded-for'], req.ip, req.ips, what does it all mean?
req.connection.remoteAddress、req.headers['x-forwarded-for']、req.ip、req.ips,这都是什么意思?
Is there a straight forward way to simply get the IP address of the client/user-agent making the request to my site in Node.js/Express? I'm not understanding all the proxy stuff or what all the differences between all the properties of the req object. Also, I don't understand what the 'trust proxy' option is for Express.
是否有一种直接的方法可以简单地获取向 Node.js/Express 中的站点发出请求的客户端/用户代理的 IP 地址?我不了解所有代理内容或 req 对象的所有属性之间的所有差异。另外,我不明白 Express 的“信任代理”选项是什么。
Could someone give me a straight forward explanation to what the difference is between all of these properties, and answer how I can just simply get the client's IP?
有人可以直接向我解释所有这些属性之间的区别,并回答我如何简单地获取客户端的 IP 吗?
回答by Dan Kohn
req.ipis the straightforward way to get the client's IP address in Express. You can see the logic it uses (which involves grabbing the first item from the array of proxy addresses req.ips, where that array is constructed from the x-forwarded-forheaders) here.
req.ip是在 Express 中获取客户端 IP 地址的直接方法。你可以看到它使用逻辑(其涉及从代理地址的阵列抓取中的第一项req.ips,其中该阵列被从构造x-forwarded-for头)这里。
回答by Svbaker
// Get client IP address from request object ----------------------
getClientAddress = function (req) {
return (req.headers['x-forwarded-for'] || '').split(',')[0]
|| req.connection.remoteAddress;
};
回答by Behnam Mohammadi
very simple
很简单
function getClientIP(req){
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}
回答by Doug
As other's have noted, due to the use potential use of proxies, you really should use req.ip and NOT use the X-Forwarded-For header like so many people are recommending. As long as you properly configure a proxy as a trusted proxy, req.ip will always return the end-user's IP address.
正如其他人所指出的,由于代理的潜在用途,您真的应该使用 req.ip 而不是像很多人推荐的那样使用 X-Forwarded-For 标头。只要您将代理正确配置为受信任的代理,req.ip 将始终返回最终用户的 IP 地址。
e.g. If you had a proxy that was connecting from 8.8.8.8, you'd do:
例如,如果你有一个从 8.8.8.8 连接的代理,你会这样做:
var express = require('express');
var app = express();
app.set('trust proxy', '8.8.8.8');
Since you trust the proxy, this would now make it so what is passed in the X-Forwarded-For header will be stored in req.ip, but ONLY if it originates from one of the trusted proxies.
由于您信任代理,因此现在可以将 X-Forwarded-For 标头中传递的内容存储在 req.ip 中,但前提是它来自受信任的代理之一。
More on trust proxy can be found here.
Now, as others have noted in the comments; especially when developing locally you may get the ip in the format of "::ffff:127.0.0.1".
现在,正如其他人在评论中指出的那样;特别是在本地开发时,您可能会以“::ffff:127.0.0.1”格式获取IP。
To always get the IPv4 Address I have:
要始终获得 IPv4 地址,我有:
getClientAddress = function (req) {
return req.ip.split(":").pop();
};
回答by Garistar
Getting the client IP is pretty straightforward:
获取客户端 IP 非常简单:
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
console.log(ip);

