Public IP not showing?

Author
Isabella Davis Author
|
21 hours ago Asked
|
3 Views
|
2 Replies
0

I am absolutely pulling my hair out with a simple web tool I launched, "What is my IP Address." It was working perfectly fine for weeks, reliably displaying the user's correct public IP address. But suddenly, out of nowhere, it has completely stopped working as intended, and I've spent the last 8 hours trying everything I can think of to fix it, with zero luck. I am completely stuck and desperately need some expert eyes on this!

The core issue is that my tool is now showing either "IP not found" or, even worse, an internal server IP like 127.0.0.1 or 10.x.x.x, instead of the user's actual public IP. It works flawlessly when I test it on my localhost development environment, but the moment it's deployed (it's a Node.js/Express app hosted on AWS EC2 behind an ALB), it fails to retrieve the correct client IP. This is incredibly frustrating because the entire purpose of the tool is to show the user their public IP!

Here's what I've tried so far, meticulously going through every possible debugging step:

  • Checked all server logs for any anomalies, errors, or unusual requests. Nothing obvious stands out related to IP retrieval.
  • Inspected HTTP headers like X-Forwarded-For and X-Real-IP. These are often either completely missing or, when present, they contain the load balancer's IP or a private IP, not the client's original public IP.
  • Tried various code methods to extract the client IP: req.ip, req.connection.remoteAddress, req.socket.remoteAddress, and custom parsing of different headers. All yield the same incorrect or private IPs.
  • Tested the tool from multiple different networks (home, mobile hotspot, office), different browsers, and even with a VPN on/off to rule out any client-side caching or network-specific issues. The problem persists everywhere.
  • Restarted the EC2 instance, the Node.js application, and even tried redeploying the entire service multiple times. No change whatsoever.

When I log the incoming request headers, this is what I typically see for the IP-related fields (or lack thereof):


// Console Output Example:
req.ip: "172.31.X.X" // A private AWS IP or the load balancer's IP
req.connection.remoteAddress: "172.31.X.X"
req.headers['x-forwarded-for']: undefined // Or sometimes, "172.31.X.X"
req.headers['x-real-ip']: undefined

What critical configuration am I overlooking here? Is there a common server/proxy setup issue on AWS (or with load balancers in general) that causes this kind of failure for public IP detection? How can I reliably obtain the true client IP in a production environment, especially when behind load balancers or CDN proxies? I'm at my wit's end.

Any expert advice or guidance would be incredibly appreciated. I'm waiting anxiously for a reply!

2 Answers

0
MD Alamgir Hossain Nahid
Answered 1 hour ago
I am absolutely pulling my hair out with a simple web tool I launched, "What is my IP Address."

It sounds like you're dealing with a classic case of what happens when a web application sits behind a reverse proxy or load balancer, which, as you've found, can be incredibly frustrating. While you're "desperately needing some expert eyes on this," perhaps a fresh pair of eyes can shed some light on this common configuration challenge.

The core issue you're experiencing is that your Node.js/Express application is seeing the IP address of the AWS Application Load Balancer (ALB), not the original client's IP. This is standard behavior, as the ALB terminates the client connection and establishes a new one to your EC2 instance. The actual client IP is passed along in the X-Forwarded-For HTTP header. Your current debugging output confirming req.ip and req.headers['x-forwarded-for'] showing private IPs or being undefined strongly indicates that your Express application isn't configured to trust the proxy.

To resolve this in an Express application, you need to explicitly tell Express to trust proxy headers. You can do this by adding a single line to your application's setup:

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

Place this line after you initialize your Express app (const app = express();) but before you define any routes or middleware that rely on client IP. When trust proxy is set to true, Express will correctly parse the X-Forwarded-For header and populate req.ip with the client's actual public IP address. If you're behind multiple proxies, you might need to set it to a number (e.g., app.set('trust proxy', 1) for one hop, or app.set('trust proxy', 'loopback') for trusted subnets). For an ALB, true usually suffices.

Additionally, for a tool specifically designed to show "What is my IP Address," relying solely on req.ip from your server might sometimes be tricky if a user is behind a complex VPN or corporate proxy protocol setup that strips or modifies headers. A robust solution often involves a fallback or primary check using an external service. You could make a client-side (browser) request to a third-party IP lookup API (like ip-api.com, ipinfo.io) or even a small endpoint on your own server that specifically queries an external service. While our own What is my IP Address tool provides this functionality, you can achieve similar results by integrating with these external APIs directly.

0
Isabella Davis
Answered 15 minutes ago

YES! That app.set('trust proxy', true); was the magic line, thank you so much! It totally fixed the public IP not showing issue, finally.

But now that it's actually working, I'm seeing something unexpected: sometimes the req.ip ends up being a CDN's IP or another proxy's IP, instead of the user's actual public IP. Is there a trick for dealing with multiple proxies and getting the real source?

Your Answer

You must Log In to post an answer and earn reputation.