Node.js:获取客户端的 IP [英] Node.js: Get client's IP
问题描述
req.connection.remoteAddress、req.headers['x-forwarded-for']、req.ip、req.ips,都是什么意思?
req.connection.remoteAddress, req.headers['x-forwarded-for'], req.ip, req.ips, what does it all mean?
是否有一种直接的方法可以简单地获取向 Node.js/Express 中的站点发出请求的客户端/用户代理的 IP 地址?我不了解所有的代理内容或 req 对象的所有属性之间的所有差异.另外,我不明白 Express 的信任代理"选项是什么.
Is there a straight forward way to simply get the IP address of the client/user-agent making the request to my site in Node.js/Express? I'm not understanding all the proxy stuff or what all the differences between all the properties of the req object. Also, I don't understand what the 'trust proxy' option is for Express.
有人可以直接向我解释所有这些属性之间的区别,并回答我如何简单地获取客户端的 IP 吗?
Could someone give me a straight forward explanation to what the difference is between all of these properties, and answer how I can just simply get the client's IP?
推荐答案
req.ip
是在 Express 中获取客户端 IP 地址的直接方法.您可以看到它使用的逻辑(包括从代理地址数组 req.ips
中获取第一项,其中该数组是从 x-forwarded-for
标头)此处.
req.ip
is the straightforward way to get the client's IP address in Express. You can see the logic it uses (which involves grabbing the first item from the array of proxy addresses req.ips
, where that array is constructed from the x-forwarded-for
headers) here.
这篇关于Node.js:获取客户端的 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!