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中,因此訪問(wèn) http://www.siguoya.name:8867/ 的時(shí)候,看到的內(nèi)容就是 test
而開(kāi)啟全局代理之后,發(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請(qǐng)求時(shí)找路徑問(wèn)題解決
當(dāng)你安裝了nginx的時(shí)候,為nginx配置了如下的location,想要去訪問(wèn)路徑下面的內(nèi)容,可是總是出現(xiàn)404,找不到文件,這是什么原因呢,今天我們就來(lái)解決這個(gè)問(wèn)題,感興趣的朋友一起看看吧2023-10-10Nginx訪問(wèn)FTP服務(wù)器文件的時(shí)效性/安全校驗(yàn)的方法
nginx的實(shí)現(xiàn)方式在校驗(yàn)失敗的時(shí)候頁(yè)面返回error image,跳轉(zhuǎn)的是420 error_page,成功的時(shí)候會(huì)訪問(wèn)FTP文件服務(wù)器的路徑,反正圖片到頁(yè)面展示,這篇文章主要介紹了Nginx訪問(wèn)FTP服務(wù)器文件的時(shí)效性/安全校驗(yàn),需要的朋友可以參考下2023-12-12Nginx服務(wù)器屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲(chóng)的方法
今天小編就為大家分享一篇關(guān)于Nginx服務(wù)器屏蔽與禁止屏蔽網(wǎng)絡(luò)爬蟲(chóng)的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例
這篇文章主要介紹了詳解Nginx中的geo模塊與利用其配置負(fù)載均衡的示例,文中對(duì)模塊的geo指令使用有比較詳細(xì)的介紹,需要的朋友可以參考下2016-01-01Nginx配置反向代理服務(wù)器實(shí)現(xiàn)在https網(wǎng)站中請(qǐng)求http資源
?Nginx反向代理?是一種將客戶端請(qǐng)求轉(zhuǎn)發(fā)到后端服務(wù)器的技術(shù),主要用于負(fù)載均衡、提高安全性和提升性能,本文給大家介紹了Nginx配置反向代理服務(wù)器實(shí)現(xiàn)在https網(wǎng)站中請(qǐng)求http資源,需要的朋友可以參考下2025-03-03nginx負(fù)載均衡配置,宕機(jī)自動(dòng)切換方式
這篇文章主要介紹了nginx負(fù)載均衡配置,宕機(jī)自動(dòng)切換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05為Nginx自定義404,502錯(cuò)誤頁(yè)面的方法
為Nginx自定義404,502錯(cuò)誤頁(yè)面的方法,需要的朋友可以參考下。2010-12-12Nginx+Keepalived實(shí)現(xiàn)雙機(jī)熱備
這篇文章主要介紹了Nginx+Keepalived實(shí)現(xiàn)雙機(jī)熱備,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06