What are the methods for Nginx to implement grayscale publishing?

PHPz
Release: 2023-05-19 23:10:05
forward
2770 people have browsed it

Method 1: By adjusting the load balancing weight

Load balancing is built on the existing network structure. It provides a cheap, effective and transparent method to expand the network equipment and server bandwidth, increase throughput, enhance network data processing capabilities, and improve network flexibility and availability.

Load balancing, the English name is load balance, means to allocate execution to multiple operating units, such as web servers, ftp servers, enterprise key application servers and other mission-critical servers, etc., so as to Complete work tasks together.

What are the methods for Nginx to implement grayscale publishing?

The simple configuration is as follows:

http { 
  upstream cluster { 
    ip_hash; #如果你的系统中没有使用第三方缓存管理工具 ,建议使用此方式
    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; 
    } 
  } 
}
Copy after login

This method of grayscale publishing is implemented through weight, but this method is only suitable for modifying the behavior of nodes, and requires The applications are all exactly the same, and their essential function is to adjust the load capacity after adding or deleting nodes. The ultimate goal is to keep the traffic balanced.

Method 2. Use nginx lua to implement grayscale 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 myip == nil then
        myip = ngx.var.remote_addr
      end
      if myip == "公司出口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;
}
Copy after login

Due to the use of nginx lua module, this method is suitable for many scenarios and is very powerful, but The problem is that you may need to learn a lot of Lua's syntax.

Method 3. Use http header information to determine the weight (grayscale value)

During the http request transmission process, user-agent, host, referer, will be automatically included. Cookie and other information. We only need to determine the IP address segment, user agent, information in cookies, etc. We take cookies as an example here.

Of course, two problems need to be solved here:

①The first visit to a static page may not generate a cookie

②We need to dynamically set the route through code

③Control the gray value through weight

We can use an example to solve the above problems of ② and ③

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 {  #通过upstream default + weight节点控制权重
    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"){ #动态控制路由
        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;
    }
 }
Copy after login

For problem ①, we can access the dynamics through script on the index page Page:

such as

<script src="https://test.taotaosou.com/cookieinfo.php" /><script>
Copy after login

In addition, we also need to determine and generate cookie

<?php

if(!session_id())
{
 session_start();
}
if(!isset($_cookie["tts_version_id"]))
{
 $cookievalue = $_server[&#39;server_port&#39;]==5280?"tts1":"tts2";
 setcookie("tts_version_id", $cookievalue, time()+3600, "/");
}
?>
Copy after login
in cookieinfo.php

The above is the detailed content of What are the methods for Nginx to implement grayscale publishing?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!