Link Search Menu Expand Document
Table of contents

GEO IP

  1. Enable geo IP by adding new module in nginx

    • Add a new module --with-http_geoip_module
    • nginx -V

      check if this module is added

  2. Adding the geo IP database from maxMind
     mkdir /etc/nginx/geoip 
    
    • download from maxMind
    • wget [download-able URL here]
    • un-pack the database, saved in the directory
  3. Configure nginx.config file

    • Add below inside http { … } block
    geoip_country /etc/nginx/geoip/GeoIP.dat;
    geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
    

Sample configuration

events { ... }
http {
        include mine.type; 

        # Enabling Geo IP 
        geoip_country /etc/nginx/geoip/GeoIP.dat;
        geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
    
    server {
        listen 80;
        server_name example.com;
        root /var/www/example.com; 

        # Configuring Geo IP
        location /geo_country {
            return 200 "visisting from: $geoip_country_name";
            # curl http://example.com/geo_country
        }

        location /geo_city {
            return 200 ""visiting from: $geoip_city";
            # curl http://example.com/geo_city
        } 
    }
}