Nginx geoip模块实现地区性负载均衡
程序员文章站
2022-06-13 19:18:54
相信做过awstats的都用过开源的geoip.dat ip数据库,刚好nginx wiki上有geoip 模块,这样就可以实现地区性的负载均衡,但是maxmi...
相信做过awstats的都用过开源的geoip.dat ip数据库,刚好nginx wiki上有geoip 模块,这样就可以实现地区性的负载均衡,但是maxmind 的ip数据库对中国的支持不算太好,不过现在也不错了~
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
geoip_city geolitecity.dat;
server {
listen 80;
server_name 75.125.197.200;
root html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
参考文章: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;
}
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;
}
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,"*"
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"
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,"*"
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 分~~
本文出自 “linuxer” 博客,请务必保留此出处http://deidara.blog.51cto.com/400447/198469
本文出自 “linuxer” 博客,请务必保留此出处http://deidara.blog.51cto.com/400447/198469
上一篇: URLScan工具配置方法第1/2页
推荐阅读