Handling cities with the same name using the GeoIP module

Maxim Dounin mdounin at mdounin.ru
Fri Nov 12 14:55:55 UTC 2021


On Fri, Nov 12, 2021 at 09:21:58AM -0500, YvorL wrote:

> I was setting up GeoIP rules
> (https://nginx.org/en/docs/http/ngx_http_geoip_module.html) and looked into
> the MaxMind database when I realized there are multiple cities that has the
> same name. This means I can't simply match $geoip_city in a rule. The only
> data that's unique and is available for all cities in the world is the
> "geoname ID" (https://www.geonames.org/). However, it seems I can't use that
> field even if it's available in the database file. Also, I don't want to
> complicate the code with different approaches for different countries (e.g.,
> metro code/DMA region code is only available in the US).
> Is there any way I can use that unique ID to identify a specific city in the
> Nginx configuration?
> If there isn't, what would be the best implementation to avoid multiple
> conditionals? Using the module already eats up memory and I'd like to
> provide the best experience for the websites.
> Examples:
> Clinton - 28 cities in the US (naturally all in different states)
> Aalst - 2 cities in Belgium (in two different provinces)
> Acailandia - 2 cities in Brazil (in two different "subdivision")
> ...
> version: nginx/1.21.3

The MaxMind database also contains "region code", available in 
nginx as $geoip_region, which identifies appropriate region within 
a country.  As far as I understand, to uniquely identify a city 
you have use $geoip_country + $geoip_region + $geoip_city.  See 
docs and the MaxMind database description for more details.

Maxim Dounin

More information about the nginx mailing list