A summary of three methods of grey level publishing implemented by Nginx

Keywords: Programming network Nginx Load Balance Web Server

A summary of three methods of grey level publishing implemented by Nginx
The main principle of gray-scale publishing is access routing control, and the key point is to ensure that each access is the same node.
Mode 1: adjust the load balance weight
Load balancing is based on the existing network structure, which provides a cheap, effective and transparent way to expand the bandwidth of network equipment and servers, increase throughput, strengthen the capacity of network data processing, and improve the flexibility and availability of the network.
Load Balance, English name is Load Balance, which means to allocate to multiple operation units for execution, such as Web server, FTP server, enterprise key application server and other key task servers, so as to jointly complete the work tasks

The simple configuration is as follows:
http { 
  upstream cluster { 
    ip_hash; #If you do not use the third-party cache management tool in your system, this method is recommended
    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 gray level as like as two peas is released through weight, but this method is only suitable for modifying the behavior of nodes, and requires applications to be exactly the same. Its essential role is to adjust the load capacity after adding or removing nodes, and ultimately, in order to make the traffic balance ultimately.

Method 2: using nginx+lua to realize gray-scale publishing of web project

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 myIP == nil then
        myIP = ngx.var.remote_addr
      end
      if myIP == "Company exports 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, very powerful, but the problem is that you may need to learn a lot of lua syntax.

Method 3: use http header information to judge + weight (gray value)
During the http request transmission, the user agent, host, referer, Cookie and other information will be brought automatically. We only need to judge ip address segment, user agent, information in Cookie, etc. Let's take cookies as an example.
Of course, there are two problems to be solved here:
① Cookies may not be generated when visiting a static page for the first time
② We need to set the route dynamically by code
③ Control gray value by weight
We can solve the problems of ② and ③ by 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 {  #Weight control through upstream default + weight nodes
    server 192.168.3.81:5280 max_fails=1 fail_timeout=60 weight=5;
    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;
    }
 }

Posted by lalabored on Sun, 26 Apr 2020 10:52:42 -0700