HttpGeoIP

This module creates ngx_http_geoip_module variables based on the IP-address of the client matched against the MaxMind GeoIP binary files. This module appeared in nginx version 0.8.6.

本模块ngx_http_geoip_module的变量基于IP地址匹配MaxMind GeoIP 二进制文件. 这个模块开始出现在nginx0.8.6。

Precondition 首先

This module needs the geo databases and the library to read the database.

模块必需有geo数据库和读取数据库类

  1. #下载免费的geo_city数据库
  2. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
  3. #下载免费的geo_coundty数据库
  4. wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
  5. #在debian中安装libgeoip:
  6. sudo apt-get install libgeoip-dev
  7. #其它系统,你可以下载并编译一个源文件
  8. wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

在centos可以用yum安装:

  1. yum install geoip

编译

  1. ./configure --with-http_geoip_module

例如

  1. http {
  2. geoip_country GeoIP.dat;
  3. geoip_city GeoLiteCity.dat;
  4. ...

指令

geoip_country

syntax:geoip_country path/to/db.dat;

default: none

context:http

The directive indicates the path to the .dat file used for determining the visitor's country from the IP-address of the client. When set the module makes available the following variables:

dat文件用于判断访问者IP中的国家。当前模块 下可用的变量 :

  • $geoip_country_code; -国家名的前两个字母, 如, "RU", "US".
  • $geoip_country_code3; - 国家名的前三个字母, 如, "RUS", "USA".
  • $geoip_country_name; -国家名称, 如, "Russian Federation", "United States".

If you only need the country's name, you can just set the geoip_country database(1.1M), while the geoip_city database is much bigger(43M) and all the databses will be cached in memory.

如果你只需要国家,你只需设置geoip_country数据库(1.1M), 但城市的ip数据库就比较大(43M)并且将加载到内存当缓存。

geoip_city

syntax:geoip_city path/to/db.dat;

default: none

context:http

The directive indicates the path to the .dat file used for determining countries, regions and cities from IP-address of the client. When set the module makes available the following variables:

dat文件用于判断访问者IP中的国家、省,城市。当前模块 下可用的变量:

  • $geoip_city_country_code; -国家名的前两个字母, 如, "RU", "US".
  • $geoip_city_country_code3; - 国家名的前三个字母, 如, "RUS", "USA".
  • $geoip_city_country_name; -国家名称, 如, "Russian Federation", "United States".
  • $geoip_region; - 省,州或区名 (province, region, state, province, federal land, and the like), 如, "Moscow City", "DC".
  • $geoip_city; - 城市名称, 如, "Moscow", "Washington".
  • $geoip_postal_code; - 邮政编号.

References

Original Documentation