How to use $geoip_city on Nginx

July 25, 2017 1.7k views
Nginx CentOS

I have nginx-1.12.1 installed on Centos 6.7 final. Already loaded the --with-httpgeoipmodule on configure arguments.

I have this code on my nginx.conf inside http context

geoip_city     /usr/local/share/GeoIP/GeoLiteCity.dat;

Also add this code

map $geoip_city $not_allowed_region {
    default no;
    Shanghai yes;

Inside my vhost conf file

# server content
if ($not_allowed_region = yes) {
   rewrite ^/(.*)$ redirect;

I did nginx -t and also restart using nginx -s reload but nothing happens.

1 Answer

Hi @meowmanijado

Are you using an updated GeoLiteCity.dat ? Also remember that a lot of connections are now IPv6, so you need to use the GeoLiteCityv6.dat:

  • I tried your suggestion to use GeoLiteCityv6.dat, but still redirection sill not working.

    geoipcountrycode is working but I need to work with geoip_city

    • @meowmanijado
      Try downloading the CSV file and find your IP-address-block to see which city it's referenced as.
      These free databases are rough and not perfect - if you want something more precise, then you need to buy a subscription to MaxMind or one of the other providers.

Have another answer? Share your knowledge.