


What 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.
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['server_port']==5280?"tts1":"tts2"; setcookie("tts_version_id", $cookievalue, time()+3600, "/"); } ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

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.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
