req.ip 和 socket.localAddress 有什么区别?
我试图了解Express Req.IP IP地址与来自节点Net https://nodejs.org/api/net.html#socketlocaladdress
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我试图了解Express Req.IP IP地址与来自节点Net https://nodejs.org/api/net.html#socketlocaladdress
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
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