Nginx结合GeoIP库

1. 编译nginx时带上geoip模块

# wget http://nginx.org/download/nginx-x.x.x.tar.gz
# tar zxvf nginx-x.x.x.tar.gz
# cd nginx-x.x.x
# ./configure --with-http_geoip_module     其余编译选项请自行填补
# make; make install

 

2. 下载可以读取GeoIP数据库的工具

# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
# tar -zxvf GeoIP.tar.gz
# cd GeoIP-1.4.8    注意这个解压出的版本随时在变,我解压时,是1.4.8版本的
# ./configure
# make; make install

上面的操作,将工具安装到了/usr/local/lib目录下,我们需要让其生效。

# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
# ldconfig

 

3. 下载GeoIP数据库

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz
# mkdir /usr/local/share/GeoIP
# mv GeoIP /usr/local/share/GeoIP/

 

4. 配置nginx,使其能够使用GeoIP库

# vi /etc/nginx/nginx.conf

geoip_country /usr/local/share/GeoIP/GeoIP.dat;

fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...

if ($geoip_country_code = CN) {
    root /data/www/;
}

 

上面只是简单的使用,更多的知识需要根据实际的情况来自行修改。所以多多看看。

官方站点:http://nginx.org/en/docs/http/ngx_http_geoip_module.html

 

转载于:https://www.cnblogs.com/t-road/p/6847146.html

你可能感兴趣的:(Nginx结合GeoIP库)