美文网首页
nodejs获取真实ip

nodejs获取真实ip

作者: 匆匆那年_海 | 来源:发表于2020-04-23 23:38 被阅读0次
    //获取真实ip 
    //是否有反向代理IP(头信息:x-forwarded-for),在判断connection的远程IP,以及后端的socket的IP
    let getClientIp = function(req) {
        return req.headers['X-Real-IP'] || 
            req.headers['x-forwarded-for'] || 
            req.connection.remoteAddress ||
             req.socket.remoteAddress ||
            req.connection.socket.remoteAddress || 
            req.ip || '';
    };
    let ip = getClientIp(req).match(/\d+.\d+.\d+.\d+/);
    ip = ip ? ip.join('.') : null;
    console.log('真实ip', ip);
    

    相关文章

      网友评论

          本文标题:nodejs获取真实ip

          本文链接:https://www.haomeiwen.com/subject/gestwhtx.html