相信做過awstats的都用過開源的geoip.dat ip數據庫,剛好nginx wiki上有geoip 模塊,這樣就可以實現地區性的負載均衡,但是maxmind 的ip數據庫對中國的支持不算太好,不過現在也不錯了~
參考文章:http://wiki.nginx.org/NginxHttpGeoIPModule
說下我的環境,我有一臺美國linux 服務器,一臺美國的windows 2003 ,一臺本的XP。機器,其他測試用戶都是,QQ羣裏的朋友,好了開始測試
linux : 75.125.x.x //美國
win2003 : 74.55.x.x // 美國
XP :localhost // 北京
測試轉發,美國用戶~轉發到 www.google.cn
電信轉發到 我的一臺 公網的 apache 默認頁面
網通轉發到 我的一臺 公網業務服務器!!
1.下載安裝nginx.
shell $> get http://sysoev.ru/nginx/nginx-0.8.13.tar.gz
shell $> tar zxvf nginx-0.8.13.tar.gz
shell $> cd nginx-0.8.13
shell $>apt-get install libgeoip-dev
shell $> ./configure --prefix=/usr/local/nginx --with-http_flv_module --user=www --group=www --with-http_gzip_static_module --with-http_geoip_module
shell $> make
shell $> make install
2.下載GeoLiteCity.dat.gz 數據庫~
shell $> wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
shell $> gzip -d GeoLiteCity.dat.gz
shell $> mv GeoLiteCity.dat /usr/local/nginx/conf/GeoLiteCity.dat
3.修改配置文件實現 地區性質負載
shell $> cd /usr/local/nginx/conf
shell $> cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
geoip_city GeoLiteCity.dat;
upstream wangtong {
server 59.151.X.X;
}
upstream dianxin {
server 75.125.X.X;
}
upstream USA {
server www.google.cn;
}
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 75.125.197.200;
root html;
index index.html index.htm;
location / {
if ($geoip_region ~ "(01|02|03|04|06|07|11|13|14|15|16|21|23|29|30|31|32|33)") {
proxy_pass http://dianxin$request_uri;
}
if ($geoip_region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)") {
proxy_pass http://wangtong$request_uri;
}
if ($geoip_city_country_code ~ "US") {
proxy_pass http://USA$request_uri;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
4.測試,用不同地方的機器做測試~
我是北京用戶,訪問
我是北京用戶訪問的是默認頁面是因爲我沒有 把 22 數字填加到 配置文件裏。我是爲了方便測試!大家要是用在生產上要把 22加到
if ($geoip_region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)")
沒有匹配到,就訪問了默認頁面~~
成都朋友幫忙訪問:
廣州的朋友幫忙訪問:
河北朋友幫忙訪問:
美國 win2003 訪問:
直接訪問 電信的 服務器 和 網通服務器
59.151.X.X; 75.125.X.X;
直接訪問 網通 59.151.X.X
直接訪問 電信服務器 75.125.X.X
下面我來解釋一下
if ($geoip_region ~ "(01|02|03|04|06|07|11|13|14|15|16|21|23|29|30|31|32|33)")
這些數字代表的是中國省份地區~~
表如下:
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"
GeoLiteCity.dat 更多變量請看 wiki 我這裏只用到兩個變量一個是$geoip_region 一個是$geoip_city_country 第一個是 地區,第二個變量是國家只取 兩個字母簡寫!
geoip_city
syntax: geoip_city path/to/db.dat;
default: none
context: http
The directive indicates the path to the .dat file used for determining countries, regions and cities from IP-address of the client. When set the module makes available the following variables:
$geoip_city_country_code; - two-letter country code, for example, "RU", "US".
$geoip_city_country_code3; - three-letter country code, for example, "RUS", "USA".
$geoip_city_country_name; - the name of the country, for example, "Russian Federation", "United States".
$geoip_region; - the name of region (province, region, state, province, federal land, and the like), for example, "Moscow City", "DC".
$geoip_city; - the name of the city, for example, "Moscow", "Washington".
$geoip_postal_code; - postal code.
PS: 我只是根據南方電信,北方網通來區分的~~ 我是北京用戶訪問的是默認頁面是因爲我沒有 把 22 數字填加到 配置文件裏。我是爲了方便測試!大家要是用在生產上要把 22加到
if ($geoip_region ~ "(05|08|09|10|12|17|18|19|20|24|25|26)")
網通裏~ 不過 開源的 geoip 還是有些不準確的~只能給他 75 分