如何从控制器中确定给定请求的IP地址?例如(快递):
app.post('/get/ip/address', function (req, res) { // need access to IP address here })
topek.. 418
在您的request
对象中有一个名为的属性connection
,它是一个net.Socket
对象.net.Socket对象有一个属性remoteAddress
,因此您应该能够通过此调用获取IP:
request.connection.remoteAddress
请参阅http和net的文档
编辑
正如@juand在评论中指出的那样,如果服务器位于代理之后,获取远程IP的正确方法是 request.headers['x-forwarded-for']
在您的request
对象中有一个名为的属性connection
,它是一个net.Socket
对象.net.Socket对象有一个属性remoteAddress
,因此您应该能够通过此调用获取IP:
request.connection.remoteAddress
请参阅http和net的文档
编辑
正如@juand在评论中指出的那样,如果服务器位于代理之后,获取远程IP的正确方法是 request.headers['x-forwarded-for']
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || (req.connection.socket ? req.connection.socket.remoteAddress : null);
请注意,有时您可以获得多个IP地址req.headers['x-forwarded-for']
.此外,x-forwarded-for
不会始终设置标头,这可能会引发错误.
该领域的一般格式是:
的x转发换: client, proxy1, proxy2, proxy3
其中值是逗号+空格分隔的IP地址列表,最左边是原始客户端,每个连续的代理通过请求添加接收请求的IP地址.在此示例中,请求通过proxy1
,proxy2
然后传递proxy3
.proxy3
显示为请求的远程地址.
这是Arnav Gupta建议的解决方案,马丁在下面的评论中为x-forwarded-for
未设置的案例提出了修正:
var ip = (req.headers['x-forwarded-for'] || '').split(',').pop() || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress
req.ip
我正在看这个,然后我就像等待,我正在使用快递.咄.
您可以保持DRY并且只使用支持IPv4和IPv6的node-ipware.
安装:
npm install ipware
在您的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(); });
它将尽最大努力获取用户的IP地址或返回127.0.0.1
以指示它无法确定用户的IP地址.查看README文件以获取高级选项.
您可以使用request-ip来检索用户的IP地址.它处理了相当多的不同边缘情况,其中一些在其他答案中提到.
披露:我创建了这个模块
安装:
npm install request-ip
在您的应用中:
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(); };
希望这可以帮助
request.headers['x-forwarded-for'] || request.connection.remoteAddress
如果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). This is because the request.connection.remoteAddress
property will contain the private ip address of the load balancer rather than the public ip address of the client. 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
.
以下函数已涵盖所有案例将有所帮助
var ip; if (req.headers['x-forwarded-for']) { ip = req.headers['x-forwarded-for'].split(",")[0]; } else if (req.connection && req.connection.remoteAddress) { ip = req.connection.remoteAddress; } else { ip = req.ip; }console.log("client IP is *********************" + ip);
function getCallerIP(request) {
var ip = request.headers['x-forwarded-for'] ||
request.connection.remoteAddress ||
request.socket.remoteAddress ||
request.connection.socket.remoteAddress;
ip = ip.split(',')[0];
ip = ip.split(':').slice(-1); //in case the ip returned in a format: "::ffff:146.xxx.xxx.xxx"
return ip;
}
获取IP地址有两种方法:
let ip = req.ip
let ip = req.connection.remoteAddress;
但是上述方法存在问题.
如果您在Nginx或任何代理后面运行您的应用程序,则每个IP地址都将是127.0.0.1
.
因此,获取用户IP地址的最佳解决方案是: -
let ip = req.header('x-forwarded-for') || req.connection.remoteAddress;
如果您使用的是快速版本3.x或更高版本,则可以使用信任代理设置(http://expressjs.com/api.html#trust.proxy.options.table),它将遍历地址链x-forwarded-for标头并将最新的ip放入您未配置为可信代理的链中,放入req对象的ip属性中.