老功能了,不多说,直接来步骤:
下载需要的文件:
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar -zxvf GeoIP.tar.gz
cd GeoIP-1.4.8/
./configure
Make
Make install
echo ‘/usr/local/lib’ > /etc/ld.so.conf.d/geoip.conf
ldconfig
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d GeoIP.dat.gz
gzip -d GeoLiteCity.dat.gz
mv GeoIP.dat GeoLiteCity.dat /usr/local/nginx/conf/
如果改完配置后哟与如下报错就需要加载geo模块,下面有介绍:
nginx: [emerg] unknown directive “geoip_country” in /etc/nginx/nginx.conf:67
动态安装geo模块, 整个过程不影响现在的Nginx访问,安装完reload nginx就可以生效了:
进入Nginx安装包目录,我的在/usr/local/src/tengine-2.1.2/下
./configure –with-http_geoip_module=shared
Make
make dso_install
然后在nginx.conf里加入:
dso {
load ngx_http_geoip_module.so;
}
#geo
geoip_country /usr/local/nginx/conf/GeoIP.dat;
geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;
加入日志中:
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for” ‘
‘”$upstream_addr” “$upstream_cache_status” ‘
‘”$upstream_status” “$upstream_response_time” “$cookie_jsessionid” “$request_time” “$
host” “–$geoip_country_name–$geoip_city–$geoip_postal_code”‘;
查看访问日志确认可以。
其它有用配置:
禁止国家访问:
if ($geoip_country_code = CN) {
deny all;
}
1 2 3 |
$geoip_country_code #两位字符的英文国家码。如:CN, US $geoip_country_code3 #三位字符的英文国家码。如:CHN, USA $geoip_country_name #国家英文全称。如:China, United States |
GeoIP 中跟国家下级区域相关的变量:
1 2 3 4 5 6 7 8 9 |
$geoip_city_country_code #也是两位字符的英文国家码。 $geoip_city_country_code3 #上同 $geoip_city_country_name #上同. $geoip_region #这个经测试是两位数的数字,如杭州是02, 上海是 23。但是没有搜到相关资料,希望知道的朋友留言告之。 $geoip_city #城市的英文名称。如:Hangzhou $geoip_postal_code #城市的邮政编码。经测试,国内这字段为空 $geoip_city_continent_code #不知什么用途,国内好像都是AS $geoip_latitude #纬度 $geoip_longitude #经度 |
网上有用来做负载均衡的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
http { include mime.types; default_type application/octet-stream; geoip_city GeoLiteCity.dat; upstream wangtong { server 59.151.X.X; } upstream dianxin { server 75.125.X.X; } upstream USA { server www.google.cn; } sendfile on; keepalive_timeout 65; server { listen 80; server_name 75.125.197.200; root html; index index.html index.htm; location / { if ($geoip_region ~ "(01|02|03|04|06|07|11|13|14|15|16|21|23|29|30|31|32|33)") { proxy_pass http://dianxin$request_uri; } if ($geoip_region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)") { proxy_pass http://wangtong$request_uri; } if ($geoip_city_country_code ~ "US") { proxy_pass http://USA$request_uri; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } |
另一个用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
http { ... geoip_city /var/GeoIP/GeoLiteCity.dat;#geoip的库文件 #handler中可以接收header中的值(Geo-City的key 等) proxy_set_header Geo-Country-Code $geoip_city_country_code; proxy_set_header Geo-Region $geoip_region; proxy_set_header Geo-City $geoip_city; proxy_set_header Geo-Postal-Code $geoip_postal_code; proxy_set_header Geo-Continent-Code $geoip_city_continent_code; proxy_set_header Geo-Latitude $geoip_latitude; proxy_set_header Geo-longitude $geoip_longitude; ... server{ ... #下面代码可以放到 location模块下 add_header geoip_city $geoip_city; add_header geoip_city_country_code $geoip_city_country_code; add_header geoip_region $geoip_region; add_header geoip_postal_code $geoip_postal_code; add_header geoip_city_continent_code $geoip_city_continent_code; add_header geoip_latitude $geoip_latitude; add_header geoip_longitude $geoip_longitude; ... } } ·$geoip_country_code - 两个字母的国家代码,如:"RU", "US"。 ·$geoip_country_code3 - 三个字母的国家代码,如:"RUS", "USA"。 ·$geoip_country_name - 国家的完整名称,如:"Russian Federation", "United States"(如果可用)。 ·$geoip_region - 地区的名称(类似于省,地区,州,行政区,联邦土地等),如:"Moscow City", "DC"(如果可用)。 ·$geoip_city - 城市名称,如"Moscow", "Washington"(如果可用)。 ·$geoip_postal_code - 邮政编码(如果可用)。 ·$geoip_city_continent_code(如果可用)。 ·$geoip_latitude - 所在维度(如果可用)。 ·$geoip_longitude - 所在经度(如果可用)。 |
在 php 中测试 GeoIP
首先需要在 fastcgi_params 或 fastcgi.conf 中引入 GeoIP 的属性,
如需Nginx传递变量给PHP,编辑fastcgi_params添加:
1 2 3 4 5 6 7 8 9 10 11 12 |
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code; fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3; fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name; fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code; fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3; fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name; fastcgi_param GEOIP_REGION $geoip_region; fastcgi_param GEOIP_CITY $geoip_city; fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code; fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code; fastcgi_param GEOIP_LATITUDE $geoip_latitude; fastcgi_param GEOIP_LONGITUDE $geoip_longitude; |
php代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php $geoip_country_code = getenv(GEOIP_COUNTRY_CODE); $geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3); $geoip_country_name = getenv(GEOIP_COUNTRY_NAME); $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE); $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3); $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME); $geoip_region = getenv(GEOIP_REGION); $geoip_city = getenv(GEOIP_CITY); $geoip_postal_code = getenv(GEOIP_POSTAL_CODE); $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE); $geoip_latitude = getenv(GEOIP_LATITUDE); $geoip_longitude = getenv(GEOIP_LONGITUDE); echo 'country_code: '.$geoip_country_code.'<br />'; echo 'country_code3: '.$geoip_country_code3.'<br />'; echo 'country_name: '.$geoip_country_name.'<br />'; echo 'city_country_code: '.$geoip_city_country_code.'<br />'; echo 'city_country_code3: '.$geoip_city_country_code3.'<br />'; echo 'city_country_name: '.$geoip_city_country_name.'<br />'; echo 'region: '.$geoip_region.'<br />'; echo 'city: '.$geoip_city.'<br />'; echo 'postal_code: '.$geoip_postal_code.'<br />'; echo 'city_continent_code: '.$geoip_city_continent_code.'<br />'; echo 'latitude: '.$geoip_latitude.'<br />'; echo 'longitude: '.$geoip_longitude.'<br />'; |
或者:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<html> <head> <title>IP地址检测,我的IP地址是多少?</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php if (getenv(HTTP_X_FORWARDED_FOR)) { $pipaddress = getenv(HTTP_X_FORWARDED_FOR); $ipaddress = getenv(REMOTE_ADDR); echo "<br>您的代理IP地址是: ".$pipaddress. " (via $ipaddress) " ; } else { $ipaddress = getenv(REMOTE_ADDR); echo "<br>您的IP地址是 : $ipaddress"; } $geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE); $geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3); $geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME); $geoip_region = getenv(GEOIP_REGION); $geoip_city = getenv(GEOIP_CITY); $geoip_postal_code = getenv(GEOIP_POSTAL_CODE); $geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE); $geoip_latitude = getenv(GEOIP_LATITUDE); $geoip_longitude = getenv(GEOIP_LONGITUDE); echo "<br>国家 : $geoip_city_country_name ( $geoip_city_country_code3 , $geoip_city_country_code ) "; echo "<br>地区 : $geoip_region"; echo "<br>城市 : $geoip_city "; echo "<br>邮政编码 : $geoip_postal_code"; echo "<br>所在洲 : $geoip_city_continent_code"; echo "<br>纬度 : $geoip_latitude "; echo "<br>经度 : $geoip_longitude "; ?> </body> </html> |
官方介绍:http://nginx.org/en/docs/http/ngx_http_geoip_module.html