nginx使用geoip判断国家

早过忘川 提交于 2020-03-09 17:27:07

Nginx可配合GeoIP模块定位IP所在物理位置并做相应处理,支持多个条件匹配:

1
2
3
4
5
6
7
8
9
10
$geoip_country_code #国家代码2位,如CN
$geoip_country_code3 #国家代码3位,如CHN
$geoip_country_name #国家完整名称,如China
$geoip_region #所在地区
$geoip_city #所在城市,如BeiJing
$geoip_postal_code #邮政编码
$geoip_city_continent_code #所在洲,如AS
$geoip_latitude #纬度
$geoip_longitude #经度

编译安装Nginx并添加GeoIP模块:

1
2
3
4
5
6
7
8
yum install geoip-devel #安装GeoIP解析库
wget http: //nginx .org /download/nginx-1 .0.15. tar .gz
tar zxvf nginx-1.0.15. tar .gz
cd nginx-1.0.15
  . /configure --user=www --group=www --prefix= /usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_geoip_module
make
make install

下载GeoIP城市国家数据库:

1
2
3
4
5
6
7
mkdir -p /usr/local/nginx/geoip
cd /usr/local/nginx/geoip
wget http: //www .maxmind.com /download/geoip/database/GeoLiteCountry/GeoIP .dat.gz
wget http: //geolite .maxmind.com /download/geoip/database/GeoLiteCity .dat.gz
gunzip GeoIP.dat.gz
gunzip GeoLiteCity.dat.gz

编辑Nginx配置文件加载GeoIP数据库:

1
2
3
4
5
http
     [...]
geoip_country  /usr/local/nginx/geoip/GeoIP.dat; #国家数据库
geoip_city     /usr/local/nginx/geoip/GeoLiteCity.dat; #城市数据库
[...]

如需Nginx传递变量给PHP,编辑fastcgi_params添加:

1
2
3
4
5
6
7
8
9
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;

应用示例,Nginx判断如果访问者IP所在国家为美国或中国,返回404错误

1
2
3
4
5
6
server
     [...]
                 if ($geoip_country_code ~* (US|CN)) {
                 #return 404;
                 }
[...]

新建PHP程序测试GeoIP:

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>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!