如果是HTTPS请求,如何在node.js中找到远程地址?
你好。在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来确实有些东西很奇怪/坏了。
从节点0.4.7开始,http似乎有remoteAddress可用:
,这两个都是未定义的,但
确实有效。
不过这个在 http 上不可用,所以你需要仔细检查。
我无法想象这种行为是故意的。
It appears something is strange/broken indeed.
As of node 0.4.7, it seems http has remoteAddress available on:
on https, both of these are undefined, but
does work.
That one isn't available on http though, so you need to check carefully.
I cannot imagine this behavior is intentional.
由于谷歌搜索“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 inapp.set("trust proxy", true);
andreq.ips
.I recommend you to read the whole discussion in the Express Google Group.
请注意,有时您可以在 req.headers['x-forwarded-for'] 中获取多个 IP 地址,特别是在使用移动电话访问您的服务器(wifi 和运营商数据)时。
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).
而且
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 withreq.headers['x-forwarded-for']
.