Skip to content

Instantly share code, notes, and snippets.

@shellus
Last active October 2, 2017 08:45
Show Gist options
  • Save shellus/0f5d529efe00ca4257e26b890798cc91 to your computer and use it in GitHub Desktop.
Save shellus/0f5d529efe00ca4257e26b890798cc91 to your computer and use it in GitHub Desktop.
Nginx use GeoIP show IP And Location Info Guide

download GeoIP databases

mkdir /etc/nginx/geoip
cd /etc/nginx/geoip
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz

load GeoIP databases in Nginx

http {
        geoip_country /etc/nginx/geoip/GeoIP.dat;
        geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
        #...
}

setting location echo info

server {
        listen         80;
        server_name domain.com;

        location / {
                add_header "Content-type" "text/html";
                echo "<pre>";
                echo "You IP:";
                echo "$remote_addr";
                echo "You Location:";
                echo "$geoip_country_name";
                echo "$geoip_region_name";
                echo "$geoip_city";
                echo "</pre>";
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment