18. Nginx and Lua gray scale release

Time:2020-5-27

Install memcached

yum install -y memcached
#Start memcached service, port 11211, start as nobody, - D background mode
memcached -p11211 -u nobody -d
#Set your own IP as the IP for grayscale test
telnet 127.0.0.1 11211
set 119.32.216.122 0 0 1
1
quit

Install 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

establish test.lua file

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

Because we will119.32.216.122This IP has been added tomemcached, so visithttp://www.siguoya.name:8867/When I saw it, I sawtest
After the global agent is turned on, the content you see isproductIn this way, the effect of gray scale test for some users is realized

Special reading

  • 1. Advantages of nginx
  • 2. Installation and startup of nginx
  • 3. Nginx directory and configuration syntax
  • 4. Nginx module
  • 5. Nginx static resource processing
  • 6. Caching principle of nginx browser
  • 7. Cross domain access to nginx resources
  • 8. Anti theft chain of nginx resources
  • 9. Nginx agent
  • 10. Nginx load balancing
  • 11. Nginx cache
  • 12. Nginx dynamic and static separation
  • 13. Nginx Rewrite
  • 14. Nginx Secure Link
  • 15. Nginx Geo
  • 16. Nginx HTTPS service
  • 17. Development of nginx and Lua
  • 18. Nginx and Lua gray scale release
  • 19. Nginx common errors
  • 20. Nginx performance optimization
  • 21. Nginx security management

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]