Nginx與Lua灰度發(fā)布的實(shí)現(xiàn)
安裝 memcached
yum install -y memcached #啟動(dòng)memcached服務(wù),端口11211,以nobody身份啟動(dòng),-d后臺(tái)模式運(yùn)行 memcached -p11211 -u nobody -d #將自己的IP設(shè)為灰度測(cè)試的IP telnet 127.0.0.1 11211 set 119.32.216.122 0 0 1 1 quit
安裝 memcached for lua
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
創(chuàng)建 test.lua 文件
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配置
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 這個(gè)IP添加進(jìn)了memcached中,因此訪問 http://www.siguoya.name:8867/ 的時(shí)候,看到的內(nèi)容就是 test
而開啟全局代理之后,發(fā)現(xiàn)看到的內(nèi)容就是 product,這樣就實(shí)現(xiàn)了針對(duì)部分用戶進(jìn)行灰度測(cè)試的效果
到此這篇關(guān)于Nginx與Lua灰度發(fā)布的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx與Lua灰度發(fā)布內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Nginx反向代理實(shí)現(xiàn)會(huì)話(session)保持的兩種方式
這篇文章主要介紹了詳解Nginx反向代理實(shí)現(xiàn)會(huì)話(session)保持的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
nginx實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)
本文給大家分享的是使用nginx實(shí)現(xiàn)代理(請(qǐng)求轉(zhuǎn)發(fā))的教程及簡單示例,非常實(shí)用,有需要的小伙伴可以參考下2017-07-07
Nginx設(shè)置HttpOnly Secure SameSite參數(shù)解決Cookie信息丟失
本文主要介紹了Nginx中Cookie缺少SameSite屬性的問題,并詳細(xì)解釋了HttpOnly、Secure和SameSite屬性的作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
nginx部署前端post請(qǐng)求405?not?allowed問題解決
在配置前端項(xiàng)目的時(shí)候遇到了一個(gè)post請(qǐng)求405 not allowed,簡單記錄一下如何配置,這篇文章主要給大家介紹了關(guān)于nginx部署前端post請(qǐng)求405?not?allowed問題解決方法,需要的朋友可以參考下2023-09-09

