req.ip 和 socket.localAddress 有什么区别?

发布于 2025-01-17 20:02:25 字数 258 浏览 3 评论 0 原文

我试图了解Express Req.IP IP地址与来自节点Net https://nodejs.org/api/net.html#socketlocaladdress

I am trying to understand what the difference is between express req.ip IP address and the local IP address that comes from node net https://nodejs.org/api/net.html#socketlocaladdress

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2025-01-24 20:02:25

socket.localAddress的值始终是服务器的IP地址,客户端连接到(ex:192.168.1.1),req.ip的值是 proxy-addr 软件包。

基于“信任代理” Express设置代理-ADDR将返回socket.remoteaddress的值,或者是从req.headers ['x-forwarded-for']中返回的值,如果存在并信任。

如果在您的设置中,您有一个代理服务器,并且“信任代理”是正确的,那么当代理中的请求会添加x-forwarded-for-for标题时,在这种情况下为socket.remoteaddress将是代理服务器和req的IP .ip将是客户端的IP(例如:74.125.127.100),如果“信任代理”为false是socket.RemoteadDress和req.ip,将是代理服务器的IP。

更多信息:
https://expressjs.com/en/guide/behind-proxies.ht-proxies.html

Value of socket.localAddress is always the IP address of the server the client is connecting to (ex: 192.168.1.1), the value of req.ip is the IP of the client as set by proxy-addr package.

Based on the "trust proxy" express setting proxy-addr will either return the value of socket.remoteAddress or a value from req.headers['x-forwarded-for'] if present and trusted.

If in your setup you have a proxy server and "trust proxy" is true then when a request comes in the proxy will add the x-forwarded-for header, in this case socket.remoteAddress will be the IP of the proxy server and req.ip will be the IP of the client (ex: 74.125.127.100), if "trust proxy" is false both socket.remoteAddress and req.ip will be the IP of the proxy server.

More info:
https://expressjs.com/en/guide/behind-proxies.html
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文