Implementation of grayscale release with Nginx and Lua

Implementation of grayscale release with Nginx and Lua

Install memcached

yum install -y memcached
#Start the memcached service, port 11211, start as nobody, -d background mode to run memcached -p11211 -u nobody -d
#Set your own IP as the grayscale test IP
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

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

Since we added the IP 119.32.216.122 to memcached, when we visit http://www.siguoya.name:8867/, we see the content test

After turning on the global proxy, I found that the content I saw was the product, which achieved the effect of grayscale testing for some users.

This is the end of this article about the implementation of grayscale release of Nginx and Lua. For more relevant content about grayscale release of Nginx and Lua, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of three methods for implementing grayscale release in Nginx

<<:  Differences between ES6 inheritance and ES5 inheritance in js

>>:  Detailed explanation of how to create an updateable view in MySQL

Recommend

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

Docker uses nextcloud to build a private Baidu cloud disk

Suddenly, I needed to build a private service for...

Vue Basic Tutorial: Conditional Rendering and List Rendering

Table of contents Preface 1.1 Function 1.2 How to...

MySQL master-slave replication principle and practice detailed explanation

Table of contents Introduction effect principle f...

Comprehensive analysis of MySql master-slave replication mechanism

Table of contents Master-slave replication mechan...

Comparison of the use of form element attributes readonly and disabled

1) Scope of application: readonly:input[type="...

MySQL partition table is classified by month

Table of contents Create a table View the databas...

Summary of common knowledge points required for MySQL

Table of contents Primary key constraint Unique p...

HTML markup language - form

Click here to return to the 123WORDPRESS.COM HTML ...

10 Underused or Misunderstood HTML Tags

Here are 10 HTML tags that are underused or misun...

Issues with locking in MySQL

Lock classification: From the granularity of data...

How to use Vue+ElementUI Tree

The use of Vue+ElementUI Tree is for your referen...