Nodejs ip address result ::1

孤街浪徒 提交于 2019-12-10 17:37:46

问题


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.

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"
})

I try yo use 3 party freamwork but result same.


回答1:


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

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

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;

check herefor more info



来源:https://stackoverflow.com/questions/42220068/nodejs-ip-address-result-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!