search
HomeOperation and MaintenanceNginxWhat are the methods for Nginx to implement grayscale publishing?

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; 
    } 
  } 
}

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;
}

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;
    }
 }

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>

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, "/");
}
?>
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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.