OS:CentOS Linux release 7.5.1804 (Core)
http_geoip_module模块,需要geoip库支持:
yum -y install geoip-devel
编译的参数:
[root@host conf]# nginx -V
nginx version: nginx/1.15.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.2j 26 Sep 2016
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.15.2 --with-http_stub_status_module --with-openssl=/root/openssl --add-module=/root/nginx-ct-1.3.1 --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-threads --with-http_realip_module --with-stream --with-http_geoip_module
下载两个ip数据包直接放在nginx的conf目录并解压:
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://www.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
在nginx.conf的http字段加载ip库:
geoip_country GeoIP.dat; geoip_city GeoLiteCity.dat;
配置文件如下
geoip_country GeoIP.dat; geoip_city GeoLiteCity.dat; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #Fujian if ($geoip_region = 07) { rewrite ^/index.html http://172.96.218.243/cn permanent; } #japan if ($geoip_country_code = "JP"){ rewrite ^/index.html http://172.96.218.243/jp permanent; } #usa if ($geoip_country_code = "US"){ rewrite ^/index.html http://172.96.218.243/us permanent; }
IP信息,具体可以在官网查询:http://www.maxmind.com/en/geoip_demo
相应的省份代码如下:
CN,01,”Anhui” CN,02,”Zhejiang” CN,03,”Jiangxi” CN,04,”Jiangsu” CN,05,”Jilin” CN,06,”Qinghai” CN,07,”Fujian” CN,08,”Heilongjiang” CN,09,”Henan” CN,10,”Hebei” CN,11,”Hunan” CN,12,”Hubei” CN,13,”Xinjiang” CN,14,”Xizang” CN,15,”Gansu” CN,16,”Guangxi” CN,18,”Guizhou” CN,19,”Liaoning” CN,20,”Nei Mongol” CN,21,”Ningxia” CN,22,”Beijing” CN,23,”Shanghai” CN,24,”Shanxi” CN,25,”Shandong” CN,26,”Shaanxi” CN,28,”Tianjin” CN,29,”Yunnan” CN,30,”Guangdong” CN,31,”Hainan” CN,32,”Sichuan” CN,33,”Chongqing”
具体查询http://dev.maxmind.com/static/maxmind-region-codes.csv,也可以通过下载csv版本的ip库手动查询,比如搜索Guanzhou,会出现,”CN”,”30″,”Guangzhou”,这个30就是省份了。
nginx之geoip模块的安装和使用