如何从控制器内确定给定请求的IP地址?例如(在快递中):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
如何从控制器内确定给定请求的IP地址?例如(在快递中):
app.post('/get/ip/address', function (req, res) {
// need access to IP address here
})
当前回答
我试过了,但都没用,
console.log(clientIp);
console.log(req.ip);
console.log(req.headers['x-forwarded-for']);
console.log(req.connection.remoteAddress);
console.log(req.socket.remoteAddress);
console.log(req.connection.socket.remoteAddress.split(",")[0]);
当在Nginx代理后运行Express应用程序时,你必须将应用程序变量信任代理设置为true。Express提供了其他一些信任代理值,您可以在它们的文档中查看这些值,但以下步骤对我来说是有效的。
app.set('trust proxy', true)
App.set('信任代理',true);
在Nginx中添加proxy_set_header X-Forwarded-For $remote_addr 服务器块的配置。
位置/ { proxy_pass http://localhost: 3001; proxy_http_version 1.1; 升级$http_upgrade; 连接“升级”; 主机$ Host; proxy_set_header X-Forwarded-For $remote_addr;#这一行 proxy_cache_bypass http_upgrade美元; }
对象中读取客户端的IP地址 req.header('x-forwarded-for')或req.connection.remoteAddress;ipfilter的完整代码
module.exports = function(req, res, next) { let enable = true; // true/false let blacklist = ['x.x.x.x']; let whitelist = ['x.x.x.x']; let clientIp = req.header('x-forwarded-for') || req.connection.remoteAddress; if (!clientIp) { return res.json('Error'); } if (enable && paths.some((path) => (path === req.originalUrl))) { let blacklist = blacklist || []; if (blacklist.some((ip) => clientIp.match(ip) !== null)) { return res.json({ status: 401, error: 'Your IP is black-listed !'}); } let whitelist = whitelist || []; if (whitelist.length === 0 || whitelist.some((ip) => clientIp.match(ip) !== null)) { next(); return; } else { return res.json({ status: 401, error: 'Your IP is not listed !'}); } } next(); };
其他回答
在你的请求对象中有一个属性叫socket,它是一个网络。套接字对象。净。套接字对象有一个属性remoteAddress,因此你应该能够通过这个调用得到IP:
request.socket.remoteAddress
(如果您的节点版本低于13,请使用已弃用的request.connection.remoteAddress)
EDIT
正如@juand在评论中指出的那样,如果服务器位于代理之后,获得远程IP的正确方法是request.headers['x-forwarded-for']
编辑2
在Node.js中使用express时:
如果你设置了app.set('信任代理',true),请请求。ip将返回真实ip地址,即使在代理。查看文档了解更多信息
要求的事情。连接已弃用node@12.12.0。使用req.connection.remoteAddress获取客户端IP可能仍然有效,但不建议使用。
幸运的是,req.socket.remoteAddress自node@0.5.10以来一直存在,是一个完美的替代品:
远程IP地址的字符串表示形式。例如,'74.125.127.100'或'2001:4860:a005::68'。如果套接字被销毁(例如,如果客户端断开连接),值可能是未定义的。
在节点10.14中,在nginx后面,你可以通过nginx头请求它来检索ip,就像这样:
proxy_set_header X-Real-IP $remote_addr;
然后在你的app.js中:
app.set('trust proxy', true);
在那之后,你想让它出现的地方:
var userIp = req.header('X-Real-IP') || req.connection.remoteAddress;
警告:
不要盲目地将其用于重要的速率限制:
let ip = request.headers['x-forwarded-for'].split(',')[0];
这很容易被欺骗:
curl --header "X-Forwarded-For: 1.2.3.4" "https://example.com"
在这种情况下,用户的真实IP地址将是:
let ip = request.headers['x-forwarded-for'].split(',')[1];
我很惊讶,没有其他答案提到这一点。
var ip = req.headers['x-forwarded-for'] ||
req.socket.remoteAddress ||
null;
请注意,有时您可以在req.headers['x-forwarded-for']中获得多个IP地址。此外,并不总是设置x-forward -for报头,这可能会抛出错误。
该字段的一般格式为:
x-forward -for: client, proxy1, proxy2, proxy3
其中的值是一个逗号+空格分隔的IP地址列表,最左边是原始客户端,每个传递请求的后续代理添加接收请求的IP地址。在本例中,请求通过proxy1、proxy2和proxy3传递。Proxy3显示为请求的远程地址。
这是Arnav Gupta提出的解决方案,Martin在下面的评论中为未设置x-forward -for的情况提出了修复建议:
var ip = (req.headers['x-forwarded-for'] || '').split(',').pop().trim() ||
req.socket.remoteAddress
使用现代JS的建议:
仅在设置时处理x-forward -for,如果设置了,则取第一个地址 其他参数使用可选链接(?.)
const parseIp = (req) =>
req.headers['x-forwarded-for']?.split(',').shift()
|| req.socket?.remoteAddress
console.log(parseIp(req))
// => 127.0.0.1