侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

linux  /  管理员 发布于 7年前   181

相信做过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.gzshell $> tar zxvf nginx-0.8.13.tar.gzshell $> cd nginx-0.8.13shell $>apt-get install libgeoip-devshell $> ./configure --prefix=/usr/local/nginx --with-http_flv_module --user=www --group=www --with-http_gzip_static_module --with-http_geoip_moduleshell $> make shell $> make install 2.下载GeoLiteCity.dat.gz 数据库~shell $> wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzshell $> gzip -d GeoLiteCity.dat.gzshell $> mv GeoLiteCity.dat /usr/local/nginx/conf/GeoLiteCity.dat 3.修改配置文件实现 地区性质负载shell $> cd /usr/local/nginx/confshell $> cat nginx.confworker_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 分~~
本文出自 “linuxer” 博客,请务必保留此出处http://deidara.blog.51cto.com/400447/198469


  • 上一条:
    nginx的FastDFS分布式存储模块测试方法
    下一条:
    nginx 目录密码保护的设置方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客