GeoIP: Lookups, Databases and Setup in nginx, Apache, CDNs and Firewalls
Short answer
GeoIP is short for geolocation by IP address: you look up an IP address in a GeoIP database or through a GeoIP API and get its estimated country, region, city, time zone and network (ISP and ASN). The result shows where the network places the address, not where the device is, so it is reliable for the country and approximate for the city. You can run a GeoIP lookup in a browser, from code, or inside web servers, CDNs and firewalls.
On this page
GeoIP turns an IP address into an approximate place: a country, usually a region and city, a time zone, and the network that owns the address. Every lookup reads a GeoIP database, either a file on your own server or a provider’s database behind an API or CDN. This page covers lookups, record fields, databases vs. APIs, and GeoIP setup in nginx, Apache, CDNs, firewalls and online stores.
The box shows your public IP address and where an edge GeoIP lookup places it. That is your IP’s GeoIP.
Your public IP address
216.73.216.128
- Location
- Columbus, Ohio, United States
- Provider
- Anthropic, PBC (AS16509)
- Time zone
- America/New_York (UTC−4)
- Coordinates
- 39.96, -83.00
This is the address websites see, and the location is an estimate that can be off by a city or more. Your device's local IP (often 192.168.x.x) is a different one and only works inside your network.
What is GeoIP?
GeoIP (geo IP location) works by address blocks. Providers assign each block to a place, using registry records, routing data, location feeds that network operators publish, network measurements and user corrections. A lookup finds the block that contains the address and returns its place: an estimate for the whole block, not for one device. The IP geolocation guide explains the data sources in detail.
GeoIP only works for public IP addresses. Private addresses such as 192.168.1.20 or 10.0.0.5 are reused in millions of home and office networks at once, so they have no location. See local vs. public IP addresses.
Typical uses are language and currency defaults, licensing and legal restrictions, country allowlists for admin logins, fraud signals (IP country vs. billing country), regional analytics, ad geo-targeting and log enrichment.
How to do a GeoIP lookup
- Your own IP: see the box above.
- Any IP address: enter an IPv4 or IPv6 address on Where am I.
- Many IPs: the batch IP lookup API takes up to 1,000 addresses per request.
GeoIP lookup from code (API)
Send the address in ip and your key in the apikey header. Leave out ip to get the location of the calling machine.
curl -H "apikey: $IPLOOKUPAPI_KEY" \
"https://api.iplookupapi.com/v1/info?ip=80.110.0.1"The response, trimmed:
{
"data": {
"ip": "80.110.0.1",
"type": "v4",
"connection": {
"asn": 8412,
"organization": "Magenta Telekom GmbH",
"isp": "Magenta Telekom",
"range": "80.110.0.0/24"
},
"location": {
"continent": { "code": "EU", "name": "Europe" },
"country": {
"alpha2": "AT",
"alpha3": "AUT",
"name": "Austria",
"is_in_european_union": true
},
"region": { "alpha2": "AT-9", "name": "Vienna" },
"city": { "name": "Vienna" },
"zip": "1010",
"latitude": 48.198674,
"longitude": 16.348388
},
"timezone": {
"id": "Europe/Vienna",
"code": "CEST",
"gmt_offset": 7200,
"is_daylight_saving": true
}
}
}Add fields=location.country.alpha2,timezone.id to get only the values you need. If the country is all you need, see the IP to country API.
GeoIP lookup on the command line
With a downloaded MMDB file, mmdblookup (package mmdb-bin on Debian and Ubuntu) looks up one address:
mmdblookup --file /usr/share/GeoIP/country.mmdb --ip 80.110.0.1 country iso_code
# prints: "AT" <utf8_string>The path after the address (country iso_code) depends on the database’s structure; leave it out to print the whole record. In Python and other languages, an MMDB reader library does the same in code. The older geoiplookup command reads only the legacy DAT format.
GeoIP location: what a GeoIP record contains
| Field | Example | Reliability |
|---|---|---|
| Country (ISO 3166-1) | AT, Austria | High |
| Region or state (ISO 3166-2) | AT-9, Vienna | Medium |
| City | Vienna | Approximate: often the nearest large city or the provider’s hub |
| Postal code | 1010 | Low: treat it as a hint |
| Latitude and longitude | 48.1987, 16.3484 | The center of an area, not a device; some sources add an accuracy radius |
| Time zone (IANA ID) | Europe/Vienna | High where a country has one time zone |
| ASN, ISP, organization | AS8412, Magenta Telekom | High: comes from routing and registry data |
| Network type | mobile, hosting, CDN | Varies by source |
| Security flags | VPN, proxy, Tor, data center | Separate datasets; quality varies |
Currency, calling code and EU membership follow from the country. For error rates and their causes, read how accurate IP geolocation is.
GeoIP database vs. GeoIP API
A GeoIP database is a file you query on your own servers. A GeoIP API queries the provider’s current database for you.
| GeoIP database | GeoIP API | |
|---|---|---|
| Updates | You download and reload each release | Always the provider’s current data |
| Hosting | File and reader on every server | Nothing to host; one HTTPS request |
| Memory | Country files are small; city files are much larger | None on your side |
| Data depth | What your edition contains; ASN and security data are often separate files | Location, ISP, ASN, time zone and more per response, by plan |
| Cost model | Free editions with attribution, or paid licenses | Free tier, then monthly plans by request volume |
| Privacy | IP addresses stay on your servers | IP addresses go to the provider; check its data processing terms |
| Latency | Local lookup, no network round trip | One round trip per uncached lookup |
| Best for | Web servers, firewalls, high-volume logs, offline systems | Apps at low to medium volume, rich data, little upkeep |
IP geolocation database formats: MMDB and CSV
- MMDB is a binary format: a search tree over the bits of the IP address plus a data section with the records. One file can cover IPv4 and IPv6. Readers map it into memory and look up addresses in-process, so web servers, firewalls and log pipelines use it.
- CSV is plain text: one row per network (CIDR or start and end address) with a country code, or with an ID that points into a separate locations file. It suits imports into SQL databases and firewalls.
- DAT is the legacy binary format of the first GeoIP libraries.
Where to download a GeoIP database
Several companies offer geo IP database downloads in MMDB and CSV, usually a free country or city edition plus paid editions with more precision and more frequent updates. Most require an account and a license key. Compare update frequency, IPv6 coverage, license terms (commercial use, redistribution, attribution) and fields, then automate the download, because address blocks keep changing owners and regions. The regional internet registries’ free daily statistics files are no substitute: their country code shows where the holder is based, not where the addresses are used.
GeoIP in nginx, Apache, CDNs, firewalls and online stores
nginx GeoIP
nginx reads MMDB files in two ways:
- Built-in
ngx_http_geoip_module: reads MMDB since nginx 1.31.7 (mainline, October 2026); in the stable 1.30 branch it reads only legacy DAT files. It is not built by default (--with-http_geoip_module). - Third-party
ngx_http_geoip2_module: works with older versions too. On Debian and Ubuntu, the packagelibnginx-mod-http-geoip2installs and enables it.
This example allows /admin/ only from two countries:
# http {} context, ngx_http_geoip2_module
geoip2 /usr/share/GeoIP/country.mmdb {
auto_reload 24h;
$geoip2_country_code default=XX country iso_code;
}
map $geoip2_country_code $admin_allowed {
default 0;
US 1;
CA 1;
}
server {
listen 80;
server_name example.com;
location /admin/ {
if ($admin_allowed = 0) {
return 403;
}
# ... your usual root or proxy_pass
}
}With the built-in module on 1.31.7 or later, write geoip_country /usr/share/GeoIP/country.mmdb; and use $geoip_country_code (see the nginx GeoIP module documentation). Behind a load balancer or CDN, nginx sees the proxy’s address: set geoip2_proxy (or geoip_proxy) to the trusted proxy ranges so the module reads X-Forwarded-For, as explained in finding the real client IP.
Apache GeoIP
Apache httpd has no GeoIP module of its own. A third-party MMDB module reads the file and copies fields such as the country code into environment variables that you name. Apache’s own Require expr then decides:
# GEO_COUNTRY is set by your MMDB module (see its documentation)
<Location "/admin">
Require expr "reqenv('GEO_COUNTRY') in { 'US', 'CA' }"
</Location>Cloudflare CF-IPCountry and other CDN headers
A CDN already runs a GeoIP lookup for each request and can pass the result to your server as a header.
| CDN | Header | How to turn it on |
|---|---|---|
| Cloudflare | CF-IPCountry: ISO country code, XX for no data, T1 for Tor | Network > IP Geolocation (all plans); city, region, time zone and coordinates via the Add visitor location headers Managed Transform |
| Amazon CloudFront | CloudFront-Viewer-Country, plus -Country-Region, -City, -Postal-Code, -Time-Zone, -Latitude, -Longitude, -ASN | Add the headers to an origin request policy |
| Google Cloud Application Load Balancer | A custom header you define, for example X-Client-Geo-Location: {client_region},{client_city} | Custom request headers on the backend service |
| Vercel | x-vercel-ip-country, plus -country-region, -city, -latitude, -longitude, -timezone, -postal-code | Sent automatically |
See Cloudflare’s IP geolocation documentation. To block or allow countries at the edge instead, write a WAF custom rule on the field ip.src.country.
Warning
Trust a CDN country header only if your server accepts traffic from the CDN alone, for example through a firewall allowlist of the CDN’s IP ranges. Otherwise anyone can send a fake CF-IPCountry header straight to your origin.
pfSense GeoIP blocking with pfBlockerNG
- Go to System > Package Manager > Available Packages and install pfBlockerNG.
- Open Firewall > pfBlockerNG > General, check Enable and click Save.
- Open the IP tab. In the GeoIP configuration section, enter the account ID and license key from a free account with the database vendor, then click Save. pfBlockerNG then updates the database daily.
- Open IP > GeoIP, edit a continent, select the countries and set Action to Deny Inbound. Click Save.
- Go to Firewall > pfBlockerNG > Update, select Update and click Run.
OPNsense GeoIP aliases
- Go to Firewall > Aliases, open the GeoIP settings tab, paste the download URL of your country database into URL and click Apply. On the Business Edition, leave URL empty.
- On the Aliases tab, add an alias, set Type to GeoIP and select the countries or whole regions. Save and apply.
- Under Firewall > Rules > WAN, add a rule with Action Block and the alias as Source.
- If the alias is too large to load, raise Firewall Maximum Table Entries under Firewall > Settings > Advanced.
Commercial firewalls call this a Geo-IP filter or region blocking. Allowing the few countries you need is easier to maintain than blocking most of the world.
Magento 2 and Adobe Commerce: GeoIP store switcher
Magento Open Source and Adobe Commerce do not pick a store view by IP on their own. GeoIP store switcher extensions detect the country, then suggest or redirect to the matching store view. On Adobe Commerce on cloud infrastructure, the included CDN module has a GeoIP option under Stores > Configuration > Advanced > System > Full Page Cache that shows a store-switch dialog or redirects, based on a country-to-store mapping. Three rules:
- Give each store view its own URL, so caches and search engines see one version per URL.
- Prefer a dialog to a redirect. Google mainly crawls from US addresses, so IP redirects can hide store views from search.
- In the EU, redirect only with the customer’s consent (see below).
GeoIP blocking: what it can and cannot do
Geo IP blocking cuts noise from automated attacks and enforces country rules, but it is not access control on its own:
- VPNs, proxies and Tor let anyone appear in an allowed country. Add authentication and rate limits, and VPN and proxy detection where fraud matters.
- Wrong locations block real users: travelers, roaming phones (their traffic often exits through the home network), company VPNs and satellite internet.
- Stale data blocks the wrong ranges. Update with every release, and cover IPv4 and IPv6.
Note
Under the EU Geo-blocking Regulation (EU) 2018/302, applicable since 3 December 2018, a trader may not block or limit EU customers’ access to its website or app for reasons of nationality, place of residence or place of establishment. It may redirect them to another country version only with their explicit consent, and the original version must stay easy to reach. Exceptions apply where EU or national law requires a restriction; sectors such as audiovisual, transport and financial services are outside its scope. Ask your counsel which rules apply to you.
GeoIP vs. GPS
| GeoIP | GPS | |
|---|---|---|
| How it works | Looks up the IP address in a database of network locations | The device measures signals from satellites |
| Precision | Country usually right; city approximate; never a street address | A few meters outdoors |
| What it needs | Only the IP address, so it works on the server for any device, even bots | A GPS receiver and, in browsers and apps, the user’s permission |
| Fooled by | VPNs, proxies, Tor, roaming, company networks | Spoofing apps or jamming |
| Typical use | Defaults, compliance, fraud signals, analytics | Navigation, delivery tracking, location sharing |
Browsers share a precise position (from GPS, Wi-Fi and cell data) only after the user agrees. GeoIP never touches the device.
Is GeoIP free?
Yes, with limits. Free GeoIP databases are usually less precise or updated less often than paid editions, and their licenses can require attribution or limit redistribution. Free GeoIP API tiers have monthly quotas: the iplookupapi Free plan includes 150 lookups per month at up to 10 per minute, with location, ISP and ASN, time zone and currency data. For more volume, compare the plans.
Frequently asked questions
What is GeoIP used for?
GeoIP is used to set defaults such as language and currency, to enforce licensing or legal restrictions by country, to allow or block countries on servers and firewalls, and to flag fraud when the IP country does not match the billing country. Marketers use it for geo-targeting and regional analytics. It needs no input from the user, but it only estimates where the network is, not where the person is.
What is the difference between GeoIP and GPS?
GeoIP estimates a location from the IP address by looking it up in a database of network locations. It needs no hardware or permission and works for any connected device, but it is only reliable at country level. GPS measures satellite signals on the device itself and is accurate to a few meters outdoors, but it needs a GPS receiver and, in browsers and apps, the user’s permission.
Is GeoIP free?
Yes, with limits. Free GeoIP databases exist, but they usually require an account, a license key and attribution, and they are less precise or updated less often than paid editions. Free GeoIP API tiers exist too: the iplookupapi Free plan includes 150 lookups per month with location, ISP and ASN, time zone and currency data. See pricing for the paid plans.
What is my IP GeoIP?
Your IP’s GeoIP is the location a GeoIP lookup assigns to your public IP address, usually a country, region and city plus your internet provider. It reflects where your provider’s network is registered and routed, which can be a nearby city rather than your own town. A VPN, proxy or company network changes the result to the location of its exit point.
How accurate is GeoIP?
GeoIP is usually correct for the country, less reliable for the region, and often off for the city, because providers assign and route addresses by network, not by household. It never shows a street address. Mobile networks, VPNs, satellite internet and recently moved address blocks are common causes of errors. The accuracy guide explains the causes in detail.
Is GeoIP blocking legal in the EU?
It depends on what you block and who you are. For online shops, the EU Geo-blocking Regulation (EU) 2018/302 has applied since 3 December 2018: traders may not block or limit EU customers’ access to their website or app, or redirect them to another country version without explicit consent, for reasons of nationality, place of residence or place of establishment. Exceptions apply where EU or national law requires a restriction, and sectors such as audiovisual and financial services are outside its scope. Check with your counsel which rules apply to you.
Look up IP addresses in your code
iplookupapi returns the location, provider, time zone and security flags of any IP address as JSON. The free plan includes 150 requests a month.
curl "https://api.iplookupapi.com/v1/info?ip=8.8.8.8" \
-H "apikey: YOUR_API_KEY"