如果是HTTPS请求,如何在node.js中找到远程地址?

发布于 2024-11-07 12:07:49 字数 151 浏览 3 评论 0原文

你好。在node.js中,如果是http请求,我可以在req.connection.remoteAddress处获取remoteAddress,

那么,如果是https请求,如何获取呢?我发现有 req.socket.remoteAddress 但我不确定。请指教。谢谢。

HI. in node.js, if it is http request, I can get the remoteAddress at req.connection.remoteAddress,

so, how to get it if https request? I find there is req.socket.remoteAddress but I'm not sure. Please advice. thanks.

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

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

发布评论

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

评论(4

极度宠爱 2024-11-14 12:07:49

看来确实有些东西很奇怪/坏了。
从节点0.4.7开始,http似乎有remoteAddress可用:

  • req.connection.remoteAddress
  • https上的req.socket.remoteAddress

,这两个都是未定义的,但

  • req.connection.socket.remoteAddress

确实有效。
不过这个在 http 上不可用,所以你需要仔细检查。
我无法想象这种行为是故意的。

It appears something is strange/broken indeed.
As of node 0.4.7, it seems http has remoteAddress available on:

  • req.connection.remoteAddress
  • req.socket.remoteAddress

on https, both of these are undefined, but

  • req.connection.socket.remoteAddress

does work.
That one isn't available on http though, so you need to check carefully.
I cannot imagine this behavior is intentional.

深居我梦 2024-11-14 12:07:49

由于谷歌搜索“express js ip”直接指向此处,因此这在某种程度上是相关的。

Express 3.0.0 alpha 现在提供了一种为客户端请求检索 IP 地址的新方法。
只需使用 req.ip。如果您正在做一些代理 jiggery-pokery,您可能会对 app.set("trust proxy", true);req.ips

我建议您阅读 Express Google 网上论坛中的整个讨论。

Since googling "express js ip" directly points to here, this is somehow relevant.

Express 3.0.0 alpha now offers a new way of retrieving IP adresses for client requests.
Simply use req.ip. If you're doing some proxy jiggery-pokery you might be interested in app.set("trust proxy", true); and req.ips.

I recommend you to read the whole discussion in the Express Google Group.

苹果你个爱泡泡 2024-11-14 12:07:49
var ip = req.headers['x-forwarded-for'] || 
     req.connection.remoteAddress || 
     req.socket.remoteAddress ||
     req.connection.socket.remoteAddress;

请注意,有时您可以在 req.headers['x-forwarded-for'] 中获取多个 IP 地址,特别是在使用移动电话访问您的服务器(wifi 和运营商数据)时。

var ip = req.headers['x-forwarded-for'] || 
     req.connection.remoteAddress || 
     req.socket.remoteAddress ||
     req.connection.socket.remoteAddress;

Note that sometimes you can get more than one ip address in req.headers['x-forwarded-for'], specially when working with mobile phones accessing your server (wifi and carrier data).

想念有你 2024-11-14 12:07:49

而且 req.headers['x-forwarded-for'] 很容易操作,因此您需要一个正确配置的代理服务器。

在使用 req.headers['x-forwarded-for'] 之前,最好先对照已知代理服务器列表检查一下 req.connection.remoteAddress

As well req.headers['x-forwarded-for'] is easily manipulated so you need a properly configured proxy server.

Is better to check req.connection.remoteAddress against a list of known proxy servers before to go with req.headers['x-forwarded-for'].

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