9

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.

Furkan Aydın
  • 99
  • 2
  • 9
  • 2
    Seems to be some configuration problem, `::1` means `localhost`. Do you use some sort of proxy mechanism? Or is your server running on your local machine? – Doe Johnson Feb 14 '17 at 07:28

1 Answers1

10

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

nikoss
  • 3,254
  • 2
  • 26
  • 40
  • I just want to point out that in your node.js app you can access the IP when behind an NGINX server like so: `req.headers['x-real-ip']` which is provided by NGINX using the configuration above. – Merunas Grincalaitis Jun 20 '23 at 12:18