Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of Business Gray Publishing in nginx based on cookie

Shulou Source: shulou.com Published: 2022-06-03 02:59:43 09月23日 Update

Cookie based on nginx to achieve business grayscale publishing

background

Grayscale publishing refers to a publishing method that can smoothly transition between black and white.

Gray level release can guarantee the stability of the whole system,

At the initial gray level, problems can be found and adjusted to ensure their influence.

Business needs to publish gray scale,

You can use nginx+lua to publish services in grayscale.

At present, this form has been realized in Guangping interactive advertising related business.

flow

After the user logs in with an account, judge whether the user account is in the list of gray scale publications, and if so, add a gray scale publication identifier to the cookie of the user, and then refresh the page.

When a user visits a page, the nginx direction proxy of the service access layer selects whether to forward the user's request to all backend machines or to a specified grayscale publishing machine according to whether the user cookie carries a grayscale identifier.

programme

The service maintains a gray-scale list of user accounts, and the corresponding identification is planted in the cookie when the gray-scale account is registered in the program.

When a user request is initiated, the nginx reverse proxy access layer determines whether the current request is sent to a full-volume server or a gray-scale server by obtaining the cookie related variables in the request.

gray scale processing logic

nginx access layer

configuration example

nginx configuration gray rules for static pages

server

{

listen 80;

server_name test.qunyingliu.qq.com;

access_log logs/test.qunyingliu.qq.com.access.log access;

Set default to full volume publishing

set $group "Full";

Determine whether there is a gray identification number in the cookie

if ($http_cookie ~* "FC_GREY=1"){

set $group Grey;

}

location / {

proxy_pass http://$group;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

index index.html index.htm;

}

}

nginx configuration greyscale rules for PHP pages

location @grey {

proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;

add_header ENV 'grey';

proxy_pass http://Grey;

}

location @full {

proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;

add_header ENV 'full';

proxy_pass http://FULL;

}

location ~ \. (php)?$ {

content_by_lua_file "conf/lua/test.qunyingliu.qq.com.lua";

}

test.qunyingliu.qq.com.lua:

local ck = require "resty.cookie"

local grey_cookie_key = "FC_GREY"

local cookie, err = ck:new()

if not cookie then

ngx.exec("@full")

else

local field, err = cookie:get(grey_cookie_key)

if not field then

ngx.exec("@full")

else

ngx.exec("@grey")

end

end

gray verification

1. Browser Console Settings Gray Cookies

console---> setCookie('FC_GREY',1)

Chrome extension: EditThisCookie--->"+"---> Add new cookie

3. Set cookies for users in the business

summary

Automatic gray scale release can only be realized with the cooperation of the service end.

The main rule is to use lua scripts on nginx

The speed and stability of requests may be affected by lua script processing,

nginx+lua is generally considered a very good match

OpenResty is also popular.

The actual effect also needs to be verified after the business is launched.

Tags: Grayscale business user identity page account rule processing access configuration list form scheme server machine script or proxy guarantee influence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Apple Shulou Tech Info Microsoft