Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of Business Gray Publishing in nginx based on cookie

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report