您当前的位置:首页 > 网站建设 > 网站维护
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax | jQuery |

Nginx与Lua灰度发布的实现

51自学网 2022-07-04 11:43:39
  网站维护

安装 memcached

yum install -y memcached#启动memcached服务,端口11211,以nobody身份启动,-d后台模式运行memcached -p11211 -u nobody -d#将自己的IP设为灰度测试的IPtelnet 127.0.0.1 11211set 119.32.216.122 0 0 11quit

安装 memcached for lua

wget https://github.com/openresty/lua-resty-memcached/archive/v0.11.tar.gztar -zxvf v0.11.tar.gzmkdir -p /usr/local/share/lua/5.1cp -r lua-resty-memcached-0.11/lib/resty /usr/local/share/lua/5.1

创建 test.lua 文件

clientIP=ngx.req.get_headers()["X-Real-IP"]if clientIP == nil then  clientIP=ngx.req.get_headers()["x_forwarded_for"]endif clientIP == nil then  clientIP=ngx.var.remote_addrend  local memcached=require "resty.memcached"  local memc,err=memcached:new()  if not memc then    ngx.say("failed to instantiate memc:",err)    return  end  local ok,err =memc:connect("127.0.0.1",11211)  if not ok then    ngx.say("failed to connect:",err)    return  end  local res,flags,err =memc:get(clientIP)  --ngx.say("value key: ",res,clientIP)  if err then    ngx.say("failed to get clientIP",err)    return  end  if res == "1" then    ngx.exec("@server_test")    return  end  ngx.exec("@server_product")

nginx配置

  server {    listen 8867;    server_name www.siguoya.name;    root /usr/local/nginx/1.12.1/html;    location / {      content_by_lua_file /etc/nginx/test.lua;    }    location /hello {      default_type text/plain;      content_by_lua 'ngx.say("hello lua")';    }    location /ua {      default_type text/plain;      content_by_lua '        clientIP=ngx.req.get_headers()["USER-AGENT"]        ngx.say("IP:",clientIP)        ';    }    location @server_product {      default_type text/plain;      return 200 'product';    }    location @server_test {      default_type text/plain;      return 200 'test';    }  }

由于我们将 119.32.216.122 这个IP添加进了memcached中,因此访问 http://www.siguoya.name:8867/ 的时候,看到的内容就是 test

而开启全局代理之后,发现看到的内容就是 product,这样就实现了针对部分用户进行灰度测试的效果


下载地址:
Ubuntu18.04 安装 Anaconda3的教程详解
使用Nginx实现灰度发布的使用
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1