Nginx ngx_http_geoip_module模块基本指令整理

  categories:nginx  author:

原创文章,转载请指明出处并保留原文url地址

本文主要针对nginx的ngx_http_geoip_module模块做简单介绍,本文具体包括如下指令:

geoip_country,geoip_city

ngx_http_geoip_module模块通过使用预编译的MaxMind数据库并且依据客户端的ip地址建变量并且赋予相应的值。

【MaxMind作用?有时你需要知道你的站点访问者来自哪个国家——比如如果你正打算执行针对地理区域的广告计划。这正是象MaxMind’s GeoIP一类的工具大显身手的地方——它可以让你从访问者的IP地址轻松获取其确切的地理位置信息。

MaxMind提供了商业数据库和免费数据库。前者更为精确,精度可以达使用者所在城市信息一级,而后者则只能确定国家和地区。在本文中,我们将演示免费版的使用方法.

http://www.maxmind.com/app/geoip_country下载免费的国家ip数据库,并将其存放于Web服务器的某个目录中。】

当使用IPv6格式的地址数据库时, IPv4方式的ip地址被作为IPv4-mapped IPv6格式进行查找。

【IPv6 and IPv4 addresses can both be represented as binary bytes. An IPv4 address fits into 4 bytes (32 bits) and IPv6 fits into 16 bytes (128 bits).

An “IPv4-Mapped” IPv6 address is mostly just an IPv6 address that has the first 96 bits set to a specific value and the 32-bit IPv4 address put into the low-order 32 bits of the IPv6 address.

The first 96 bits are broken into two parts: The high-order 80 bits are all zeros, and bits 81-96 are x’FFFF’. In IPv6 notation, then, an IPv4 address of 10.0.1.170 would be an IPv4-mapped IPv6 address of ::FFFF:10.0.1.170. (Consecutive nodes of all zeros may be compressed in the notation as simly “::”.)】

这个模块不是默认的内建模块,这个模块需要采用–with-http_geoip_module产生来打开编译选项。

这个模块需要MaxMind GeoIP库。

配置示例如下:

    http {

        geoip_country         GeoIP.dat;

        geoip_city            GeoLiteCity.dat;

        geoip_proxy           192.168.100.0/24;

        geoip_proxy           2001:0db8::/32;

        geoip_proxy_recursive on;

        …

Nginx原文:

The ngx_http_geoip_module module (0.8.6+) creates variables whose values depend on the client IP address, using the precompiled MaxMind databases.

When using IPv6 databases (1.3.12), IPv4 addresses are looked up as IPv4-mapped IPv6 addresses.

This module is not built by default, it should be enabled with the –with-http_geoip_module configuration parameter.

This module requires the MaxMind GeoIP library.

Example Configuration

    http {

        geoip_country         GeoIP.dat;

        geoip_city            GeoLiteCity.dat;

        geoip_proxy           192.168.100.0/24;

        geoip_proxy           2001:0db8::/32;

        geoip_proxy_recursive on;

        …

1. geoip_country

syntax:

geoip_country   file;

default:

context:

http

指定一个数据库,根据客户端ip地址及这个数据库来决定访问用户来自的国家。当使用一个合适的数据库后,下面的一些变量可以使用。

$geoip_country_code

两个字母的国家代码,例如“RU", “US”

$geoip_country_code3

三个字母的国家代码,例如:“RUS”, “USA”

$geoip_country_name

国家名称,例如“Russian Federation”, “United States”.

Nginx原文:

Specifies a database used to determine a country depending on the client IP address. The following variables are available when using this database:

$geoip_country_code

two-letter country code, for example, “RU”, “US”.

$geoip_country_code3

three-letter country code, for example, “RUS”, “USA”.

$geoip_country_name

country name, for example, “Russian Federation”, “United States”.

2. geoip_city

syntax:

geoip_city  file;

default:

context:

http

制定一个根据用户ip地址来查找的数据库,根据这个数据库决定访问来自的国家,地区及城市。下面的变量可以使用。

$geoip_area_code

电话号码区号(仅适用美国)

这个变量可能包括一些过时的数据(由于相应数据库包括一些废弃的数据的缘故)

$geoip_city_continent_code

两个字母的大陆代码, 例如 “EU”, “NA”.

$geoip_city_country_code

两个字母的国家代码 例如:“RU”, “US”.

$geoip_city_country_code3

三个字母的国家代码,例如:“RUS”, “USA”.

$geoip_city_country_name

国家名称,例如:“Russian Federation”, “United States”.

$geoip_dma_code

    DMA区域代码(在美国) (众所周知也叫做“metro code”), 根据Google AdWords API.

$geoip_latitude

纬度。

$geoip_longitude

经度。

$geoip_region

两个符号的国家代码(地区, 领域, 国家, 省, 联邦土地等), 例如:“48”, “DC”.

$geoip_region_name

国家地区名称 (地区, 领域, 国家, 省, 联邦土地等), 例如, “Moscow City”, “District of Columbia”.

$geoip_city

城市名称, 例如: “Moscow”, “Washington”.

$geoip_postal_code

邮政编码

Nginx原文:

Specifies a database used to determine a country, region, and city depending on the client IP address. The following variables are available when using this database:

$geoip_area_code

    telephone area code (US only).

        This variable may contain outdated information since the corresponding database field is deprecated.

$geoip_city_continent_code

    two-letter continent code, for example, “EU”, “NA”.

$geoip_city_country_code

    two-letter country code, for example, “RU”, “US”.

$geoip_city_country_code3

    three-letter country code, for example, “RUS”, “USA”.

$geoip_city_country_name

    country name, for example, “Russian Federation”, “United States”.

$geoip_dma_code

    DMA region code in US (also known as “metro code”), according to the geotargeting in Google AdWords API.

$geoip_latitude

    latitude.

$geoip_longitude

    longitude.

$geoip_region

    two-symbol country region code (region, territory, state, province, federal land and the like), for example, “48”, “DC”.

$geoip_region_name

    country region name (region, territory, state, province, federal land and the like), for example, “Moscow City”, “District of Columbia”.

$geoip_city

    city name, for example, “Moscow”, “Washington”.

$geoip_postal_code

    postal code.

 

3. geoip_org

syntax:

geoip_org   file;

default:

context:

http

本指令出现在1.0.3版本中

制定一个用于根据客户端ip地址来判断客户端组织的数据库。选择合适的数据库后下面的指令可以使用。

$geoip_org

组织的名称,例如, “The University of Melbourne”.

Nginx原文:

This directive appeared in version 1.0.3.

Specifies a database used to determine an organization depending on the client IP address. The following variable is available when using this database:

$geoip_org

organization name, for example, “The University of Melbourne”.

4. geoip_proxy

syntax:

geoip_proxy  address| CIDR;

default:

context:

http

本指令出现在1.3.0和1.2.1版本中。

定义了信任地址。当一个请求来自一个值得信赖的地址,从“x-forwarded-for”请求标头字段将被用信任地址代替(本人英语水平有限不一定能准确理解这个翻译,请谅解)。

Nginx原文:

This directive appeared in versions 1.3.0 and 1.2.1.

Defines trusted addresses. When a request comes from a trusted address, an address from the “X-Forwarded-For” request header field will be used instead.

5. geoip_proxy_recursive

syntax:

geoip_proxy_recursive   on | off;

default:

geoip_proxy_recursive off;

context:

http

本指令出现在1.3.0和1.2.1版本中。

如果递归搜索功能被关闭,则客户端发送“X-Forwarded-For”字段中最后的地址将被用来作为可信赖地址来代表这个客户端的原始地址。如果递归搜索功能开启,“X-Forwarded-For”字段中最后一个不被信任的地址将被一个匹配的信任地址替换,作为这个客户端的原始地址。

Nginx原文:

This directive appeared in versions 1.3.0 and 1.2.1.

If recursive search is disabled then instead of an original client address that matches one of the trusted addresses, the last address sent in “X-Forwarded-For” will be used. If recursive search is enabled then instead an original client address that matches one of the trusted addresses, the last non-trusted address sent in “X-Forwarded-For” will be used.



快乐成长 每天进步一点点      京ICP备18032580号-1