0

I want to get the IP adress of the client that visits my page. My app is running behind a proxy so i set this configuration to my nginx file:

proxy_set_header X-Forwarded-For $remote_addr;

Now i try to get the IP adress like this in express

req.ip
req.headers["x-forwarded-for"]
req.ips
req.connection.remoteAdress

Nothing seems to work. I also set trust proxy to true

app.set("trust proxy", true);

But the other configuration like proxy_set_header Host or proxy_set_header Connection "upgrade" are working. What am i doing wrong?

bill.gates
  • 101
  • 4
  • What version of nginx do you use? Check furthest down on this site: https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/ – Orphans Aug 11 '20 at 09:07
  • 1
    @Orphans versoin nginx/1.10.3, i will your suggestions real quick – bill.gates Aug 11 '20 at 09:08

1 Answers1

0

add this config to your nginx config =>

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

and in express you can get ip with this

  let ip = req.headers['x-forwarded-for'] ||
            req.connection.remoteAddress ||
            req.socket.remoteAddress ||
            (req.connection.socket ? req.connection.socket.remoteAddress : null);