nginx如何实现灰度

从这个组网可以看到 https://www.cnblogs.com/takemetoyourheart/p/12684018.html  , 我们需要在nginx上通过灰度策略, 使得部分测试用户可以访问到机房B,也就是灰度机房。

1、在nginx的vhost文件中

location = /reverse-proxy {

  internal;

  proxy_set_header Host www.xxx.com;

  proxy_set_header X-Forwarded-For $http_x_forwarded_for;

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  proxy_set_header X-Real-IP $remote_addr;

  proxy_set_header Connection "";

  proxy_http_version 1.1;

  add_header X-Frame-Options "SAMEORIGIN";

  set $verify_return_page $http_referer;

  rewrite_by_lua_file '/opt/test/etc/lua/flow_control/access.lua';

  set_by_lua_file $backend '/opt/test/etc/lua/grey/grey.lua' $backend;

  proxy_pass http://${backend}${request_uri};

}

upstream.conf中设置的目的IP分成两种

upstream xx {

  server 100.100.100.1:8443;

}

upstream xx-grey {

  server 100.100.100.2:8443;

}

2、lua脚本

事先要定义grep.ip文件,把灰度的IP都加入进来,形如

10.10.10.1

10.10.10.2

源IP要看网站策略。如果用到CDN,那么就用CDN约定的源IP。否则就是remote_addr

local ipfile='/opt/test/etc/lua/grey/grey.ip'

ngx.shared.g_grey_ip:flush_all()

if ip == nil then

  ip = ngx.var.remote_addr

end

if ip ~=nil and ngx.shared.g_grey_ip:get(ip) then

   backend = backend..'-grey'

end

这样就实现了在nginx 内嵌入lua脚本,实现灰度IP访问灰度机房的策略。

原文地址:https://www.cnblogs.com/takemetoyourheart/p/12684298.html