By wdphd


2014-10-26 11:46:30 8 Comments

I have my express server running on port 3000 with nginx for the reverse proxy.

req.ip always returns 127.0.0.1 and req.ips returns an empty array

app.enable('trust proxy');

With/without enabling trust proxy, x-forwarded-for doesn't work:

var ip_addr = req.headers['X-FORWARDED-FOR'] || req.connection.remoteAddress;

nginx configuration:

server {
    listen 80;
    server_name localhost;
    access_log /var/log/nginx/dev_localhost.log;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

How do i get the IP address of the requesting client?

2 comments

@Lance 2018-10-01 16:23:07

According to the express documentation:

Express behind proxies

When running an Express app behind a proxy, set (by using app.set()) the application variable trust proxy to one of the values listed in the following table.

You can set it to a boolean, ip address, number or a custom function. If you want to just get the client's proxy to your express app's req.ip, you can just set it to true.

app.set('trust proxy',true); 
app.get("/", (req, res)=>{
    console.log(req.ip); 
}); 

@mscdex 2014-10-26 16:06:29

You need to pass the appropriate X-Forwarded-For header to your upstream. Add these lines to your upstream config:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

@wdphd 2014-10-26 16:57:35

This works! It's working on my server. But its still printing 127.0.0.1 on localhost

@mscdex 2014-10-26 16:59:13

If you're connecting to localhost from localhost what else would you expect?

@Atul Agrawal 2016-06-09 10:30:05

Not Working for me

@Dat30 2018-04-30 19:35:20

var ip = (req.headers["cf-connecting-ip"]) ? req.headers["cf-connecting-ip"] : 'unknown'; if any of you are using cloudflare

@user1955934 2018-07-11 05:04:32

can't figure out how to get the request headers set in nginx conf stackoverflow.com/questions/51265803/…

@Anthony Manning-Franklin 2019-03-01 08:02:06

this is frustrating to test on localhost, it seems to not check the x-forwarded-for on local requests, but i'm trying to spoof a request through our stack while developing locally... am i correct or going mad?

Related Questions

Sponsored Content

9 Answered Questions

[SOLVED] (13: Permission denied) while connecting to upstream:[nginx]

2 Answered Questions

[SOLVED] How to forward request IP from NGINX to node.js application?

2 Answered Questions

Keycloak Redirect url with nginx is going to http rather than https

  • 2018-04-03 12:02:15
  • Atulya Nair
  • 1543 View
  • 4 Score
  • 2 Answer
  • Tags:   nginx jboss

2 Answered Questions

1 Answered Questions

Nginx win server 2012 - Configure proxy pass

  • 2017-07-31 15:02:09
  • johns193
  • 338 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx proxy

2 Answered Questions

How to point many paths to proxy server in nginx

1 Answered Questions

[SOLVED] Node+Express+NGINX application returning localhost instead of domain

1 Answered Questions

Wordpress constant redirect with nginx upstream

Sponsored Content