nodejs获取真实ip

2020-04-23  本文已影响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);
上一篇下一篇

猜你喜欢

热点阅读