nginx geoip 模块实现地区性负载均衡

简介:
说下我的环境,我有一台美国linux 服务器,一台美国的windows 2003 ,一台本的XP。机器,其他测试用户都是,QQ群里的朋友,好了开始测试
linux : 75.125.x.x                    //美国
win2003 : 74.55.x.x                // 美国
XP :localhost                     // 北京
 
测试转发,美国用户~转发到  www.google.cn
电信转发到 我的一台 公网的 apache 默认页面
网通转发到  我的一台 公网业务服务器!!
 
1.下载安装nginx.
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 $> 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 分~~


本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/198469,如需转载请自行联系原作者
相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
1月前
|
应用服务中间件 nginx
Nginx安装nginx-rtmp-module模块
【2月更文挑战第4天】 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。 nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。
74 6
|
16天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
32 0
|
6天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
21 2
|
27天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0
|
1月前
|
存储 缓存 负载均衡
【Apache ShenYu源码】如何实现负载均衡模块设计
整个模块为ShenYu提供了什么功能。我们可以看下上文我们提到的工厂对象。/***/核心方法很清晰,我们传入Upsteam列表,通过这个模块的负载均衡算法,负载均衡地返回其中一个对象。这也就是这个模块提供的功能。
18 1
|
2月前
|
负载均衡 Java 应用服务中间件
|
2月前
|
负载均衡 监控 应用服务中间件
Nginx负载均衡:你的网站流量翻倍利器
Nginx负载均衡:你的网站流量翻倍利器
43 0
|
2月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
63 0
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
95 0