


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

AI Hentai Generator
Generate AI Hentai for free.

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 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 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.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

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.

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.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.
