How CDNs and anycast break IP geolocation
By Parneet6 min read
If you geolocate the IP address of a request that arrived through a CDN, you will get a location. It will be confident, well-formed, and meaningless.
This is worth understanding properly, because the failure is not the database being wrong. It is a question that has no single correct answer being asked anyway.
Anycast: one address, many places
Normally an IP address is announced to the internet from one place. Anycast breaks that assumption deliberately: the same address is announced from dozens of locations at once, and each network routes to whichever is closest to it.
That is the whole point. A user in Sydney and a user in Frankfurt both connect to the same address and reach different physical machines, with no redirection and no DNS trickery.
Now ask a geolocation database where that address is. There is no answer. It is in Sydney. It is also in Frankfurt, São Paulo, and thirty other cities, simultaneously. Whatever the database returns is either one arbitrary location or the provider's registered head office, and neither tells you anything about the user.
What this breaks in practice
Geolocating your own edge nodes. If you look up the address your CDN serves from, you learn nothing about the visitor.
Server-side location on CDN-fronted traffic. If your origin sees the CDN's address rather than the client's, every request appears to come from wherever the CDN is — often a single location for your entire audience.
DNS resolver addresses. Public resolvers are heavily anycast. Geolocating the resolver that made a query tells you about the resolver, not the person.
Getting the real client address
The fix is not better geolocation data. It is asking the right address.
A CDN or reverse proxy passes the original client address in a header. Which
header depends on the provider — X-Forwarded-For is the common one, and most
CDNs also set their own. Your framework almost certainly has a trusted-proxy
configuration that handles this.
Do not read X-Forwarded-For naively. It is a client-supplied header and
trivially forged. Anyone can set it to any value. It is only meaningful if you
trust the proxy that appended it, which means:
- Know which proxy addresses you trust
- Take the address your trusted proxy added, not the leftmost value in the chain
- Fall back to the socket address when the header is absent or unexpected
Getting this wrong is not merely inaccurate. If you use the header for rate limiting or access control without validating the proxy, you have handed control of it to the caller.
Sanity-checking that you are looking at the right thing
The hosting flag is a quick indicator. If you are geolocating what you believe
to be residential users and a large share come back as datacenter addresses,
you are almost certainly reading a proxy address rather than a client address:
{
"privacy": { "hosting": true },
"asn": { "number": 13335, "organization": "Cloudflare, Inc." }
}
An ASN belonging to a CDN or cloud provider, on traffic that should be residential, is the signal. It is a much faster diagnosis than puzzling over why everyone appears to be in one city.
The narrower point
Geolocation answers "where is this address used". For an anycast address, that question is malformed — it is used in many places by design. For a proxy address, the answer is correct but about the wrong machine.
Neither is a data quality problem, and no provider can fix either. The fix is on your side: resolve the client address first, then geolocate that.
Every lookup here returns the ASN and a hosting flag alongside the location,
which is usually enough to spot a proxy address immediately — see the
API documentation. If you want the broader picture of what geolocation
can and cannot tell you, start with
how accurate IP geolocation really is.