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

How to view files in Docker image

How to view files in a docker image 1. If it is a...

How to create your first React page

Table of contents What is Rract? background React...

Vue implements a visual drag page editor

Table of contents Drag and drop implementation Dr...

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157 Pref...

Nginx prohibits direct access via IP and redirects to a custom 500 page

Directly to the configuration file server { liste...

A brief discussion on creating cluster in nodejs

Table of contents cluster Cluster Details Events ...

MySQL database introduction: detailed explanation of database backup operation

Table of contents 1. Single database backup 2. Co...

About the problem of offline installation of Docker package on CentOS 8.4

The virtual machine used is CentOS 8.4, which sim...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

Examples of using MySQL pessimistic locking and optimistic locking

Pessimistic Lock Pessimistic lock, considers the ...

How to delete the container created in Docker

How to delete the container created in Docker 1. ...