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