时间:2021-05-02
安装 memcached
? 1 2 3 4 5 6 7 8 yum install -y memcached #启动memcached服务,端口11211,以nobody身份启动,-d后台模式运行 memcached -p11211 -u nobody -d #将自己的IP设为灰度测试的IP telnet 127.0.0.1 11211 set 119.32.216.122 0 0 1 1 quit安装 memcached for lua
? 1 2 3 4 wget https://github.com/openresty/lua-resty-memcached/archive/v0.11.tar.gz tar -zxvf v0.11.tar.gz mkdir -p /usr/local/share/lua/5.1 cp -r lua-resty-memcached-0.11/lib/resty /usr/local/share/lua/5.1创建 test.lua 文件
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 clientIP=ngx.req.get_headers()["X-Real-IP"] if clientIP == nil then clientIP=ngx.req.get_headers()["x_forwarded_for"] end if clientIP == nil then clientIP=ngx.var.remote_addr end 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配置
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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,这样就实现了针对部分用户进行灰度测试的效果
到此这篇关于Nginx与Lua灰度发布的实现的文章就介绍到这了,更多相关Nginx与Lua灰度发布内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://segmentfault.com/a/1190000022194409
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
公司业务有用到Lua,与nginx结合起来实现高性能的web应用,想去弄懂这方面的知识,就去了解学习了下Lua,欢迎大咔们指教。1.Lua介绍Lua是一门简单的
Nginx实现灰度发布的三种方法总结灰度发布的主要原理是访问路由的控制,重点是保证每次访问的是同一个节点。方式一:通过调节负载均衡权重负载均衡建立在现有网络结构
本文介绍如何用Nginx+Lua+Redis来构建高并发Web应用,Curl请求Nginx,Nginx通过Lua查询Redis,返回json数据。一、安装1、安
最近有个工作是使用Nginx+Lua实现一个操作MongoDB数据库的API,主要实现其count和query功能。之前没有写过Lua,于是也就勉强着上手,在c
Nginx很强Lua很强所以,他俩和一起也一定很强。。。。(多么霸道的逻辑呀~)lua_nginx_module可以一步步的安装,也可以直接用淘宝的OpenRe