15. Nginx Geo

新方法

http {
    ...
     geoip2 /etc/nginx/vendor/GeoLite2-Country.mmdb {
        auto_reload 5m;
        $geoip2_metadata_country_build metadata build_epoch;
        $geoip2_data_country_code default=US source=$remote_addr country iso_code;
        $geoip2_data_country_name country names en;
    }

    geoip2 /etc/nginx/vendor/GeoLite2-City.mmdb {
        $geoip2_data_city_name default=London city names en;
    }

    fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
    fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
    fastcgi_param CITY_NAME    $geoip2_data_city_name;
    ....
    
    server {
        listen 8081;
        server_name www.siguoya.name;
        location / {
            if ($geoip2_data_country_code != 'CN'){
                return 403;
            }
            default_type text/plain;
            return 200 "$remote_addr $geoip2_data_country_code $geoip2_data_country_name $geoip2_data_city_name";
       }
   }
}

访问:http://www.siguoya.name:8081/

113.111.3.206 CN China Guangzhou

旧方法

由于 maxmind 不再提供 dat 格式的数据下载以及维护,以下方法已废弃

IP数据库下载地址:
国家数据库:http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
城市数据库:http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

http {
  geoip_country /path/to/GeoIP.dat;
  geoip_city /path/to/GeoLiteCity.dat;
  server{
    listen 3592;
    location / {
      default_type text/plain;
      if ($geoip_country_code != 'CN'){
        return 403;
      }
      return 200 "$remote_addr $geoip_city_country_name $geoip_country_code $geoip_city";
    }
  }
}

访问:http://www.siguoya.name:3592/

119.32.216.122 China CN Guangzhou

专题阅读

posted on 2020-03-30 18:41  思过崖灬  阅读(277)  评论(0编辑  收藏  举报

导航