Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of grayscale release of Nginx and Lua

Shulou Source: shulou.com Published: 2022-06-02 03:40:33 10月04日 Update

Install memcached

yum install -y memcached#Start memcached service, port 11211, start as nobody, -d background mode run memcached -p11211 -u nobody -d#Set IP to greyscale IPtelnet 127.0.0.1 11211set 119.32.216.122 0 0 11quit

Install 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

Create test.lua file

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 configuration

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'; } }

Since we added the IP www.example.com to memcached, when visiting http://www.siguoya.name:8867/, what we see is test. 119.32.216.122

After the global proxy is turned on, it is found that the content you see is product, which achieves the effect of gray scale testing for some users.

This article on the implementation of Nginx and Lua gray published article on this, more related Nginx and Lua gray published content please search for previous articles or continue to browse the following related articles hope that you will support a lot later!

Tags: Grayscale content article that is test global background effect file time more mode user port identity part faceted proxy search support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Huawei NVidia Shulou Tech Info