How to Get the Client IP Address in Express

Express exposes a request's client address through req.ip. With the default trust proxy setting, that value comes from the network socket. If the app sits behind a reverse proxy, the socket peer is usually the proxy. Enabling proxy trust allows Express to use addresses from X-Forwarded-For. The trust setting must match the real request path to avoid accepting a forged client address.
The selected address can affect logs, rate limits, and access decisions. A visitor can send arbitrary forwarding headers to an app that accepts direct traffic. A proxy can also relay a visitor-supplied header unless it removes or replaces untrusted values. Map every path into Express before changing trust proxy, and test both normal proxied requests and an untrusted direct request with a forged header.
Map All Paths to the Express App
List the layers between the visitor and Express: CDN, load balancer, local reverse proxy, service mesh, or container ingress. Record which component connects directly to the application and which addresses it uses. Include exceptional paths such as a health checker, internal service, staging route, or direct port mapping. The topology matters because Express walks the forwarding chain from the socket peer toward the reported client.
Identify which proxy writes X-Forwarded-For and how the proxies handle values supplied by visitors. At the first trusted proxy, record the address of the connecting client instead of treating an unchecked incoming value as the visitor address. Later proxies may append the peer addresses they observe.
Also check X-Forwarded-Host and X-Forwarded-Proto, which can affect the hostname and protocol Express reports. Ensure the final proxy passes only the values your deployment intends to trust. Forwarding arbitrary client input in those fields can influence more than the displayed IP address.
Select a Narrow Trust Configuration
Express accepts several forms for trust proxy: a Boolean, an address or subnet list, a hop count, or a custom function. An explicit list of known proxy addresses is often easier to audit when the network topology is known. Express checks whether the socket address is trusted, then examines X-Forwarded-For from right to left until it finds the first untrusted address. That address becomes the effective client for req.ip.
For example, suppose Express receives connections from a proxy at 192.0.2.20, with another controlled proxy at 192.0.2.10 before it. After creating your Express application object, configure the trusted addresses with app.set('trust proxy', ['192.0.2.20', '192.0.2.10']); before the application handles requests.
These are documentation addresses, not values to copy into a real deployment. Replace them with the actual proxies in your request path. Trusting an entire private network can include unrelated workloads that should not be allowed to supply client addresses.
Do not copy app.set('trust proxy', true) without understanding its effect. With a Boolean true setting and a nonempty forwarding header, Express treats the leftmost forwarded address as the client. If the final proxy has not removed or overwritten a visitor-supplied header, the visitor may choose that value. A numeric hop count is also risky when the app can be reached through paths of different lengths: a shorter path can let client-controlled values occupy a trusted position.
Use a trust function only when there is a clear reason to encode a more specific policy and it can be maintained. Keep proxy address lists current when a CDN or load balancer changes. Restrict direct network access to the app when that is part of the architecture, but still test how the application behaves if a direct path exists unexpectedly.
Read and Interpret req.ip
Read req.ip inside a request handler or middleware function. In a controlled test environment, the following middleware records the selected address, the socket peer, and the processed forwarding list before passing the request to the next handler:
app.use((req, res, next) => {
console.log({
clientIp: req.ip,
socketIp: req.socket.remoteAddress,
forwarded: req.ips
});
next();
});Place the diagnostic middleware before the routes you want to inspect. Keep its output restricted and remove the temporary logging when testing is complete. The socket value identifies the immediate peer; it is expected to be a proxy on the normal path.
The req.ips array includes forwarded addresses up to the point where Express stops applying proxy trust, ordered from the selected client toward the app. It excludes the socket peer and can include the first untrusted address, so it is not simply a list of trusted proxies. If no forwarded address is accepted, the array is empty.
In the two-proxy example, suppose the socket peer is 192.0.2.20 and X-Forwarded-For contains 203.0.113.44, 192.0.2.10. With both proxies trusted, req.ip is 203.0.113.44 and req.ips contains 203.0.113.44 followed by 192.0.2.10. If only 192.0.2.20 is trusted, processing stops at 192.0.2.10, which becomes the selected client address.
Do not treat the leftmost value in X-Forwarded-For as automatically trustworthy, and do not split the header manually for access control. Express's trust logic exists to apply the configured boundary while traversing the chain. The result is still an observed network address, not proof of a person's identity or physical location. NAT, VPNs, and shared egress points can all change the apparent address.
Test Proxied and Direct Requests
Send a request through the normal public route from a known test client. Check that the socket peer is one of the expected proxies and that req.ip matches the address reported by the sanitized chain. Test every alternate ingress path, including health checks and routes through different proxies. A configuration that is safe on one path may fail on another.
In an authorized test environment, send a direct request to the app with a forged X-Forwarded-For header. Confirm that an untrusted connection cannot make req.ip become the invented value. If the direct port is deliberately blocked, verify that the network block works and separately exercise the trust logic in a controlled setup. Record the Express version, trust setting, topology, expected result, and observed result without exposing personal addresses.
When the test fails, fix the proxy's header handling or the Express trust policy rather than adding a broader address range to make a log look right. Review any rate limits, audit logs, or IP-based restrictions that use req.ip, because they inherit the same trust decision.
Review Trust Settings after Network Changes
Document the trusted peers and why each belongs in the list. After adding a CDN, changing ingress, or enabling a new direct route, compare the actual peer addresses with the configuration and repeat the request tests. Keep only the address data needed for operations under the site's retention rules.







