nginx做白名单以及灰度流量控制

首先是nginx.conf

user root root;
worker_processes 4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include gated_launch.conf;
    server {
        listen       80;
        server_name  localhost;
        location / {
            add_header gated_launch $gated_launch;
            add_header white_list $white_list;
            root   html;
        }
    }
}

gated_launch.conf

split_clients "${arg_uid}" $gated_launch {
  50% true;
  50% false;
}

map $arg_uid $white_list {
  20 true;
  50 true;
  60 true;
  5678 true;
  default false;
}

调用方访问以下接口,即可知道该请求是否在白名单以及灰度名单中。

127.0.0.1:80/?uid=1

注意端口号后面的/是必须的,不然拿不到header。

response只包含header,如下:

HTTP/1.1 200 OK

Server: nginx/1.9.3

Date: Thu, 12 Nov 2015 03:25:56 GMT

Content-Type: text/html

Content-Length: 0

Last-Modified: Thu, 12 Nov 2015 02:11:36 GMT

Connection: keep-alive

ETag: "5643f558-0"

gated_launch: true

white_list: false

Accept-Ranges: bytes

gated_launch 表示是否在灰度名单。

white_list 表示是否在白名单。

原文地址:https://www.cnblogs.com/longzhaoyu/p/4958724.html