本文翻译自:How to determine a user's IP address in node
How can I determine the IP address of a given request from within a controller? 如何从控制器中确定给定请求的IP地址? For example (in express): 例如(快递):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
#1楼
参考:https://stackoom.com/question/Y1Ds/如何在节点中确定用户的IP地址
#2楼
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null);
Note that sometimes you can get more than one IP address in req.headers['x-forwarded-for']
. 请注意,有时您可以在req.headers['x-forwarded-for']
获得多个IP地址。 Also, an x-forwarded-for
header will not always be set which may throw an error. 此外,不会始终设置x-forwarded-for
标头,这可能会引发错误。
The general format of the field is: 该领域的一般格式是:
x-forwarded-for: client, proxy1, proxy2, proxy3
x-forwarded-for: client, proxy1, proxy2, proxy3
where the value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. 其中值是逗号+空格分隔的IP地址列表,最左边是原始客户端,每个连续的代理通过请求添加接收请求的IP地址。 In this example, the request passed through proxy1
, proxy2
, and then proxy3
. 在此示例中,请求通过proxy1
, proxy2
和proxy3
。 proxy3
appears as remote address of the request. proxy3
显示为请求的远程地址。
This is the solution suggested by Arnav Gupta with a fix Martin has suggested below in the comments for cases when x-forwarded-for
is not set : 这是Arnav Gupta建议的解决方案, 马丁在下面的评论中提出了修复x-forwarded-for
未设置的情况:
var ip = (req.headers['x-forwarded-for'] || '').split(',').pop() ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress
#3楼
You can stay DRY and just use node-ipware that supports both IPv4 and IPv6 . 您可以保持DRY并且只使用支持IPv4和IPv6的 node-ipware 。
Install: 安装:
npm install ipware
In your app.js or middleware: 在您的app.js或中间件中:
var getIP = require('ipware')().get_ip;
app.use(function(req, res, next) {
var ipInfo = getIP(req);
console.log(ipInfo);
// { clientIp: '127.0.0.1', clientIpRoutable: false }
next();
});
It will make the best attempt to get the user's IP address or returns 127.0.0.1
to indicate that it could not determine the user's IP address. 它将最好地尝试获取用户的IP地址或返回127.0.0.1
以指示它无法确定用户的IP地址。 Take a look at the README file for advanced options. 查看README文件以获取高级选项。
#4楼
如果您使用的是快速版本3.x或更高版本,则可以使用信任代理设置( http://expressjs.com/api.html#trust.proxy.options.table ),它将遍历地址链x-forwarded-for标头并将最新的ip放入您未配置为可信代理的链中,放入req对象的ip属性中。
#5楼
You can use request-ip , to retrieve a user's ip address. 您可以使用request-ip来检索用户的IP地址。 It handles quite a few of the different edge cases, some of which are mentioned in the other answers. 它处理了相当多的不同边缘情况,其中一些在其他答案中提到。
Disclosure: I created this module 披露:我创建了这个模块
Install: 安装:
npm install request-ip
In your app: 在您的应用中:
var requestIp = require('request-ip');
// inside middleware handler
var ipMiddleware = function(req, res, next) {
var clientIp = requestIp.getClientIp(req); // on localhost > 127.0.0.1
next();
};
Hope this helps 希望这可以帮助
#6楼
request.headers['x-forwarded-for'] || request.connection.remoteAddress
If the x-forwarded-for
header is there then use that, otherwise use the .remoteAddress
property. 如果x-forwarded-for
标头在那里,则使用它,否则使用.remoteAddress
属性。
The x-forwarded-for
header is added to requests that pass through load balancers (or other types of proxy) set up for HTTP or HTTPS (it's also possible to add this header to requests when balancing at a TCP level using proxy protocol ). x-forwarded-for
标头被添加到通过为HTTP或HTTPS设置的负载平衡器(或其他类型的代理)的请求中(当使用代理协议在TCP级别进行平衡时,也可以将此标头添加到请求中)。 This is because the request.connection.remoteAddress
the property will contain the private IP address of the load balancer rather than the public IP address of the client. 这是因为request.connection.remoteAddress
属性将包含负载均衡器的私有IP地址,而不是客户端的公共IP地址。 By using an OR statement, in the order above, you check for the existence of an x-forwarded-for
header and use it if it exists otherwise use the request.connection.remoteAddress
. 通过使用OR语句,按上面的顺序,检查是否存在x-forwarded-for
标头,如果存在则使用它,否则使用request.connection.remoteAddress
。