概述
基于IP地址匹配MaxMind GeoIP二进制文件,读取IP所在地域信息。
安装模块
yum install nginx-module-geoip
使用场景
(1)区别国内外作HTTP的访问规则(国内访问国内服务器,国外访问国外服务器)
(2)区别国内城市地域作HTTP访问规则(可作就近访问规则)
测试
(1)首先需要在/etc/nginx/nginx.conf
中加载GeoIp模块
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
#.....
(2)下载两个数据文件GeoIp.dat和GeoLiteCity.dat
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Tip:没翻墙可以使用迅雷下载。
(3)解压放入指定位置,我放在/etc/nginx/geoip/
下,使用gzip -d XXX.gz
解压。
(4)修改配置文件,/etc/nginx/conf.d/test_geo.conf
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
location / {
if ($geoip_country_code != CN){
return 403;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
上面是需要改动的地方:
(1)加载GeoIP数据文件
(2)增加访问规则
用处:基于地域不同或国家不同,可进行访问控制,进行proxy_pass
等。