Node.js IP地址结果:: 1 [英] Nodejs ip address result ::1

查看:150
本文介绍了Node.js IP地址结果:: 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常有趣的问题.我有一个网站,我想获取客户端的IP地址.我找到了一些孤独的地方,但没有一个起作用.我正在使用Nginx.

I have a realy interesting problem. I have a web site and i want to get client ip address. I found some solition but none of them work. I am using nginx.

我正在使用expressjs

i am using expressjs

app.post("/api/test",(req, res)=>{
console.log(req.header('x-forwarded-for')) // result "::1"
console.log(req.connection.remoteAddress) // result "::1"
console.log(req.ip) // result "::1"
})

我尝试使用3个聚会做伴,但结果相同.

I try yo use 3 party freamwork but result same.

推荐答案

如果您在本地主机上工作,这是正常的尝试将其登录到服务器上,您将获得用户的地址.

If you are working on localhost this is normal try logging this on server you will get the address of the user.

否则,在这种情况下,您可能会在节点服务器之前运行nginx或类似的反向代理,则应设置适当的标头

Or you might be running nginx or similar reverse proxy in front of your node server in this case you should set proper headers

对于nginx,您需要这个

for nginx you need this ones

proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

此处中查看

这篇关于Node.js IP地址结果:: 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆