码迷,mamicode.com
首页 > 其他好文 > 详细

基于Geoip城市的灰度发布

时间:2016-08-10 22:17:33      阅读:759      评论:0      收藏:0      [点我收藏+]

标签:


1.安装epel源

wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

 2.首先安装 MaxMind 的 GeoIP 库,其官网是: http://www.maxmind.com,MaxMind 提供了免费的 IP 地域数据库(GeoIP.dat),不过这个数据库文件是二进制的

yum install GeoIP GeoIP-update GeoIP-devel -y

 3.nginx安装

./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 		#优化参数,可不选

make
make install

4.测试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, 0

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz #重新下载新的城市库
cp GeoLiteCity.dat GeoLiteCity.dat.bak
gunzip GeoLiteCity.dat

geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 202.96.134.133  #正常获取城市
GeoIP City Edition, Rev 1: CN, 30, Guangdong, Guangzhou, N/A, 23.116699, 113.250000, 0, 0

5.配置nginx.conf

vim /usr/local/nginx/conf/nginx.conf
......
include vhost/www.conf;  
......

6.配置www.conf

# 加载ip库
geoip_country  /usr/share/GeoIP/GeoLiteCountry.dat;
geoip_city     /usr/share/GeoIP/GeoLiteCity.dat;

# 地址库解析,国家代码2位,国家代码3位,国家名称
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;

# 所在地区,所在城市
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;

# 邮政编码,所在洲,维度,经度
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;

# 灰度版本
upstream huiduserver {
        server 183.2.191.195:8202 weight=1;
        }

# 稳定版本
upstream server {
        server 183.2.191.195:8201 weight=1;
        }

server {
        listen       80;
        server_name  huidu.pin.cn project.pin.com;

        # 默认
        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;

                # 灰度版本
                if ($geoip_city ~* Guangdong) {
                        proxy_pass http://huiduserver;
                }
                if ($geoip_region = 30) {
                        proxy_pass http://huiduserver;
                }

                # 稳定版本
                proxy_pass http://server;
        }

 7.指定ip灰度发布

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;

if ($remote_addr ~ "101.95.155.134") {
  proxy_pass http://huiduserver;
}
  proxy_pass http://cgiservers;
} 

8.指定US和CN国家返回404

server  
      if ($geoip_country_code ~* (US|CN)) {  
      #return 404;  
     }   

9. 参数说明

$geoip_country_code       #国家代码2位,如CN  
$geoip_country_code3      #国家代码3位,如CHN  
$geoip_country_name       #国家完整名称,如China  
$geoip_region          #所在地区  
$geoip_city            #所在城市,如BeiJing  
$geoip_postal_code      #邮政编码  
$geoip_city_continent_code #所在洲,如AS  
$geoip_latitude        #纬度  
$geoip_longitude        #经度

10.相应的省份代码

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”

===============================

参考资料:

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

 

 

 

基于Geoip城市的灰度发布

标签:

原文地址:http://www.cnblogs.com/sunmmi/p/5758134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!