借住Nginx实现负载均衡和灰度发布的功能

2019-07-15 18:05:01 浏览数 (1)

Nginx实现灰度发布的功能

1. 对URL进行单独的过滤发布

代码语言:javascript复制
upstream A {
        ip_hash;
        server 10.0.XX.XX:9000;
	    server 10.0.XX.XX:9010;
    }
upstream B {	
	    #server 10.0.5.76:9000;
        server 10.0.5.76:9010;
}
server {
        listen       80;
        server_name  www.xxxx.com;
        #charset koi8-r;
        access_log  logs/host.xxx.log  main;

        location / {
            proxy_pass   http://A;
			proxy_set_header  Host            $host; 
			proxy_set_header  X-Real-IP        $remote_addr; 
			proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; 
            index  index.html index.htm;
        } 
       	location ~ /print/xxxInfo {
		 	proxy_pass   http://B;
			proxy_set_header  Host            $host; 
			proxy_set_header  X-Real-IP        $remote_addr; 
			proxy_set_header  X-Forwarded-For                    $proxy_add_x_forwarded_for;
			 proxy_read_timeout 150;
          } 	
	#location ~ /mps/xxxInfo {
	#			return 500;
	#		}
	##开启后直接封掉某个请求。
    #location ~ /mps/xxxB {
	#			return 500;
	#}    
    }	

2. 针对IP地址进行过滤

代码语言:javascript复制
如果是内部IP,则反向代理到hilinux_02(预发布环境);如果不是则反向代理到hilinux_01(生产环境)。

upstream hilinux_01 {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}

upstream hilinux_02 {
    server 192.168.1.200:8080 max_fails=1 fail_timeout=60;
}

upstream default {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}

server {
  listen 80;
  server_name  www.hi-linux.com;
  access_log  logs/www.hi-linux.com.log  main;

  set $group default;
  if ($remote_addr ~ "211.118.119.11") {
      set $group hilinux_02;
  }

 location / {
    proxy_pass http://$group;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    index  index.html index.htm;
  }
}

0 人点赞