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?
Related Questions
Sponsored Content
9 Answered Questions
[SOLVED] (13: Permission denied) while connecting to upstream:[nginx]
- 2014-05-30 06:37:10
- Mulagala
- 137832 View
- 257 Score
- 9 Answer
- Tags: django python-2.7 nginx gunicorn
2 Answered Questions
2 Answered Questions
2 Answered Questions
[SOLVED] what's wrong with this configuration for nginx as reverse proxy for node.js?
- 2011-05-19 02:57:16
- Brian Park
- 4389 View
- 6 Score
- 2 Answer
- Tags: node.js nginx reverse-proxy
1 Answered Questions
2 Answered Questions
How to point many paths to proxy server in nginx
- 2014-10-22 18:16:44
- elzix88
- 724 View
- 0 Score
- 2 Answer
- Tags: url tomcat nginx proxy reverse-proxy
2 comments
@Lance 2018-10-01 16:23:07
According to the express documentation:
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.
@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:@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
fromlocalhost
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?