nginx流量分割和加header

用户画像

运维人员经常遇到这样的需求:业务进行灰度发布,90%的流量会被正式版本的后台服务处理,10%的流量会被尝鲜版后台服务处理。设置这个尝鲜版既可以用于β测试,也相当于用户参与需求评审。各位打王者荣耀和LOL的时候不都是有个体验服。这个体验服就是尝鲜版。只不过有的公司是随机把用户流量加入到尝鲜版的。
本教程就是满足生产环境流量按比例随机导流到体验服这一需求的。

nginx配置

nginx有个按比例分配流量的功能,通过ngx_http_split_clients_module模块实现,该模块可通过客户端的某些属性对客户端通过hash算法按比例分配,这些属性包括客户端ip等,通过hash函数,将不同客户端ip进行比例分配,从而可以将部分流量引入新版本服务中,下面看一下具体配置:

http {
    
    
	# nginx log配置
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$http_Authorization" $request_body';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    # 配置keepalived时间,允许一次会话无流量传输1分钟后关闭
    keepalive_timeout  65;

    #gzip  on;

    # 允许headers中包含下划线。
    underscores_in_headers on;

    # 增大内存缓存buffers,防止流量header过大而被写入文件,磁盘IO瓶颈导致网络卡顿。
    # 增加内存缓存之后,大header的流量也放在内存里进行处理,不动用磁盘。
    # 目前的服务器,内存都很大,而且内存便宜。磁盘速度慢,有擦写上限,网络流量走磁盘容易把磁盘整坏。
    client_header_buffer_size 128k;
    client_body_buffer_size 1m;
    proxy_buffer_size 32k;
    proxy_buffers 64 32k;
    proxy_buffering on;
    proxy_busy_buffers_size 1m;
    proxy_temp_file_write_size 1m;

	# 假如我么有个后台服务叫helloworld ,是用kubernetes部署的。现在我们为这个服务添加一个尝鲜版。
	# 首先为尝鲜版添加一个新的k8s deployment
    # 将线上小程序的流量分成两部分,发送到mp-api和mp-grey两个上游,比例可调。mp-api是正式版,mp-grey是尝鲜版。
    split_clients "$remote_addr" $helloworld_backend {
    
    
        10%   helloworld_grey_server
        *     helloworld_official_server
    }

    upstream helloworld_official_server{
    
    
        server helloworld_official-svc.applet.svc.cluster.local:11001 max_fails=3 fail_timeout=10s weight=100;
        server helloworld_official-svc.applet:11001 max_fails=3 fail_timeout=10s weight=100;
    }

    upstream helloworld_grey_server{
    
    
        server helloworld-grey-svc.applet.svc.cluster.local:12001 max_fails=3 fail_timeout=10s weight=100;
        server helloworld-grey-svc.applet:12001 max_fails=3 fail_timeout=10s weight=100;
    }

	limit_req_zone  $binary_remote_addr zone=one:10m rate=50r/s;#限流配置 限制了每秒只接受某个ip 5次每秒的请求频率
    limit_req_zone  $http_Authorization zone=auth:10m rate=50r/s;#限流配置 限制了每秒只接受每个用户token 5次每秒的请求频率
    limit_conn_zone $limit zone=conn:10m;#限流配置 限制连接数

	server {
    
    
        listen       80;
        listen  [::]:80;

        #charset koi8-r;
        access_log  /var/log/nginx/access.log  main;

        location / {
    
    
            limit_req zone=one burst=1;#burst参数为超过频率限制的最大ip数量,如果超出burst,则其他ip再超出频率就直接返回503
            return 200;
            #proxy_pass  http://sentry-ip:9000;
        }

		# 开始记笔记了,同学们,重点来了!!!!!!!!!!!!!!!!!!!!!!!!!
		location /helloworld/v1/ {
    
    
            # 分流,如果是尝鲜版,新增一个请求头作为标记
            if ($helloworld_backend ~ "helloworld_grey_server") {
    
    
                proxy_set_header grey "yes";
            }
            limit_req zone=auth burst=100;#burst参数为超过频率限制的最大ip数量,如果超出burst,则其他ip再超出频率就直接返回503
            proxy_pass  http://$helloworld_backend/;
            proxy_set_header Host $host:9002;
            #故障转移
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429 non_idempotent;
            #将请求传递到下一个服务器可以通过尝试次数和时间来限制,限制请求可以传递到下一个服务器的时间。 0值关闭此限制。
            proxy_next_upstream_timeout 0;
            #限制将请求传递到下一个服务器的可能尝试次数。 0值关闭此限制。
            proxy_next_upstream_tries 0;
            proxy_read_timeout 50s;
            proxy_connect_timeout 50s;
            proxy_send_timeout 100s;
        }

猜你喜欢

转载自blog.csdn.net/qq_43626147/article/details/125094877