What are the methods for Nginx to realize gray scale publishing?

12-19-2023

Mode 1: By adjusting the load balancing weight

Load balancing is based on the existing network structure, which provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase the throughput, strengthen the network data processing capacity, and improve the flexibility and availability of the network.

Load balance, named in English as load balance, means to allocate it to multiple operating units for execution, such as web servers, ftp servers, enterprise key application servers and other key task servers, so as to jointly complete the work tasks.


Simple configuration is as follows:

http { upstream cluster { ip_hash; # If you don't use a third-party cache management tool in your system, it is recommended to use this method: server 192.168.1.210:80 weight=5;  server 192.168.1.211:80 weight=3;  server 192.168.1.212:80 weight=1;  } server { listen 80;  location / { proxy_next_upstream error timeout;  proxy_redirect off;  proxy_set_header host $host;  #proxy_set_header x-real-ip $remote_addr;  proxy_set_header x-real-ip $http_x_forwarded_for;  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;  client_max_body_size 100m;  client_body_buffer_size 256k;  proxy_connect_timeout 180;  proxy_send_timeout 180;  proxy_read_timeout 180;  proxy_buffer_size 8k;  proxy_buffers 8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://cluster; } } }

This way of gray scale publishing is realized by weight, but this way is only suitable for modifying the behavior of nodes, and it requires that all applications are exactly the same. Its essential function is to adjust the load capacity after nodes are added or deleted, and the ultimate goal is to keep the traffic balanced.

Method 2. Using nginx+lua to realize the gray publishing of web projects.

location / { content_by_lua ' myip = ngx.req.get_headers()["x-real-ip"] if myip == nil then myip = ngx.req.get_headers()["x_forwarded_for"] End if my IP = = nilthenmy IP = ngx.var.remote _ addr end if my IP = = "company export IP" then ngx.exec ("@ client") else ngx.exec ("@ client _ test") end'; } location @client{ proxy_next_upstream error timeout;  proxy_redirect off;  proxy_set_header host $host;  #proxy_set_header x-real-ip $remote_addr;  proxy_set_header x-real-ip $http_x_forwarded_for;  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;  client_max_body_size 100m;  client_body_buffer_size 256k;  proxy_connect_timeout 180;  proxy_send_timeout 180;  proxy_read_timeout 180;  proxy_buffer_size 8k;  proxy_buffers 8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client; }location @client_test{ proxy_next_upstream error timeout;  proxy_redirect off;  proxy_set_header host $host;  #proxy_set_header x-real-ip $remote_addr;  proxy_set_header x-real-ip $http_x_forwarded_for;  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;  client_max_body_size 100m;  client_body_buffer_size 256k;  proxy_connect_timeout 180;  proxy_send_timeout 180;  proxy_read_timeout 180;  proxy_buffer_size 8k;  proxy_buffers 8 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_pass http://client_test; }

Because of the use of nginx+lua module, this method is suitable for many scenarios and very powerful, but the problem is that you may need to learn a lot of lua grammar.

Mode 3. Using http header information to judge+weight (gray value)

In the process of http request transmission, information such as user-agent,host,referer,cookie and so on will be automatically brought. We only need to judge the ip address segment, the user agent, the information in the cookie and so on. We take cookie as an example here.

Of course, there are two problems to be solved here:

① Visiting a static page for the first time may not generate cookie.

② We need to dynamically set the route through code.

③ Control the gray value by weight.

We can solve the problems of ② and ③ mentioned above through an example.

upstream tts_v6 { server 192.168.3.81:5280 max_fails=1 fail_timeout=60; }upstream tts_v7 { server 192.168.3.81:5380 max_fails=1 fail_timeout=60; }upstream default {# Control the weight server192.168.3.81: 5280max _ failures = 1fail _ timeout = 60wt = 5 through the upstream default+weight node;  server 192.168.3.81:5380 max_fails=1 fail_timeout=60 weight=1; }server { listen 80;  server_name  test.taotaosou.com ;  access_log logs/test.taotaosou.com.log main buffer=32k;  #match cookie set $group "default"; If ($ http _ cookie ~ * "TTS _ version _ id = tts1") {# Dynamic control routing set $group tts_v6;  } if ($http_cookie ~* "tts_version_id=tts2"){ set $group tts_v7;  } 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 ; } }

For question ①, we can access the dynamic page through script on the index page:

such as

In addition, we also need to judge and generate cookie in cookieinfo.php.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us