centos 7 上爲nginx 增長Geo IP的功能nginx
yum install gcc gcc-c++ make automake autoconf libtool wget unzip -y if [ ! -d "/usr/local/software" ]; then mkdir /usr/local/software fi cd /usr/local/software if [ ! -f "/usr/local/software/libmaxminddb-1.3.2.tar.gz" ]; then wget http://download.zhufunin.com/libmaxminddb-1.3.2.tar.gz fi if [ ! -f "/usr/local/software/ngx_http_geoip2_module.zip" ]; then wget http://download.zhufunin.com/ngx_http_geoip2_module.zip fi if [ ! -f "/usr/local/software/maxmind-city.mmdb.tar.gz" ]; then wget http://download.zhufunin.com/maxmind-city.mmdb.tar.gz fi tar zxvf libmaxminddb-1.3.2.tar.gz tar zxvf maxmind-city.mmdb.tar.gz unzip -o ngx_http_geoip2_module.zip cd ./libmaxminddb-1.3.2 ./configure make && make install [[ -z `cat /etc/ld.so.conf |grep "\/usr\/local\/lib"` ]] && echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig
安裝nginx的時候c++
./configure \centos
...bash
--add-module=/usr/local/software/ngx_http_geoip2_module-master \spa
...code
在nginx 配置http的位置添加如下配置,maxmind-city.mmdb IP庫須要放到/usr/local/nginx/geoip/下面blog
fastcgi_param COUNTRY_CODE $geoip2_data_country_code; fastcgi_param COUNTRY_NAME $geoip2_data_country_name; fastcgi_param CITY_NAME $geoip2_data_city_name; geoip2 /usr/local/nginx/geoip/maxmind-city.mmdb { $geoip2_data_country_code default=US source=$remote_addr country iso_code; $geoip2_data_country_name country names en; $geoip2_data_city_name default=London city names en; $geoip2_data_province_name subdivisions 0 names en; $geoip2_data_province_isocode subdivisions 0 iso_code; }
maxmind IP 庫更新腳本,能夠放到crontab 中按期運行,更新IP庫crontab
#!/bin/bash cd /usr/local/nginx/geoip/ rm -rf /usr/local/nginx/geoip/GeoLite2-City_* rm -rf /usr/local/nginx/geoip/GeoLite2-City.tar.gz* wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz code="$?" echo $code if test "$code" -ne 0 ;then echo "Download is failed" exit 0; fi tar zxf GeoLite2-City.tar.gz cd GeoLite2-City_* if test ! -e GeoLite2-City.mmdb; then exit 0; fi size=`du -s ./GeoLite2-City.mmdb |awk '{print $1}'` if test "$size" -lt 50000;then exit 0; fi echo "mv GeoLite2-City.mmdb maxmind-city.mmdb" mv GeoLite2-City.mmdb maxmind-city.mmdb echo "mv maxmind-city.mmdb /usr/local/nginx/geoip/" mv maxmind-city.mmdb /usr/local/nginx/geoip/