How to use $geoip_city on Nginx

Posted July 25, 2017 3.3k views

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.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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.