1. Installing the Epel source
wget HTTPS://DL.FEDORAPROJECT.ORG/PUB/EPEL/6/X86_64/EPEL-RELEASE-6-8.NOARCH.RPMRPM-IVH epel-release-6-8.noarch.rpm
2. First install MaxMind's GeoIP Library, its official website is: Http://www.maxmind.com,MaxMind provides a free IP Geo database (GeoIP.dat), but this database file is binary
Yum Install GeoIP geoip-update geoip-devel-y
3.nginx Installation
./configure--prefix=/usr/local/nginx-1.10.1--user=nginx--group=nginx--with-pcre--with-threads--with-http_ssl_ Module--with-http_stub_status_module--with-http_sub_module--with-http_gzip_static_module--with-http_realip_ Module--with-http_geoip_module--with-google_perftools_module #优化参数, optional makemake install
4. Test GeoIP
Geoiplookup-f/usr/share/geoip/geolitecity.dat 202.96.134.133 #获取不到城市GeoIP City Edition, Rev 1:CN, N/A, n/A, n/A, n/a , 34.772499, 113.726601, 0, 0wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz #重新下载新的城市库cp GeoLiteCity.dat GeoLiteCity.dat.bakgunzip geolitecity.datgeoiplookup-f/usr/share/geoip/geolitecity.dat 202.96.134.133 #正常获取城市GeoIP City Edition, Rev 1:cn, 0, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0
5. Configure nginx.conf
Vim/usr/local/nginx/conf/nginx.conf......include vhost/www.conf; ......
6. Configure www.conf
# Load IP Library geoip_country/usr/share/geoip/geolitecountry.dat;geoip_city/usr/share/geoip/geolitecity.dat;# address library parsing, Country code 2 bits, country code 3 bits, country name Fastcgi_param geoip_city_country_code $geoip _city_country_code;fastcgi_param geoip_city_country _code3 $geoip _city_country_code3;fastcgi_param geoip_city_country_name $geoip _city_country_name;# area, located in City fastcgi _param geoip_region $geoip _region;fastcgi_param geoip_city $geoip _city;# zip Code, continent, dimension, longitude Fastcgi_param Geoip_postal_ CODE $geoip _postal_code;fastcgi_param geoip_city_continent_code $geoip _city_continent_code;fastcgi_param GEOIP_ LATITUDE $geoip _latitude;fastcgi_param geoip_longitude $geoip _longitude;# grayscale version upstream Huiduserver {server 183.2.1 91.195:8202 weight=1; }# Stable version upstream server {server 183.2.191.195:8201 weight=1; }server {Listen 80; server_name huidu.pin.cn project.pin.com; # default location/{Proxy_redirect off; Proxy_set_header Host $host; Proxy_set_header X-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; # grayscale Version if ($geoip _city ~* Guangdong) {Proxy_pass http://huiduserver; } if ($geoip _region =) {Proxy_pass http://huiduserver; } # Stable version Proxy_pass http://server; }
7. Specify IP Grayscale Publishing
8. Specify US and CN countries to return 404
Server if ($geoip _country_code ~* (us| CN) { #return 404; }
9. Parameter description
$geoip _country_code #国家代码2位, such as CN $geoip _country_code3 #国家代码3位, such as CHN $geoip _country_name # National full name, such as China $geoip _region #所在地区 $geoip _city #所在城市, such as Beijing $geoip _postal_code #邮政编码 $geoip _city_continent_code #所在洲, as $geoip _latitude #纬度 $geoip _longitude #经度
10. The corresponding province code
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"
===============================
Resources:
Http://nginx.org/en/docs/http/ngx_http_geoip_module.html
http://www.cnlvzi.com/index.php/Index/article/id/157
https://www.zybuluo.com/zypo/note/282612
Http://www.verydemo.com/demo_c167_i13565.html
Gray-scale publishing based on GeoIP City