Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of grayscale publishing in Nginx

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the methods for Nginx to achieve grayscale release". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Method 1: by adjusting the weight of load balancing

Load balancing is based on the existing network structure, which provides a cheap, effective and transparent way to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capacity, and improve network flexibility and availability.

Load balancer, known as load balance in English, means that it is distributed to multiple operating units for execution, such as web server, ftp server, enterprise critical application server and other critical task servers, so as to complete the task together.

The simple configuration is as follows:

Http {upstream cluster {ip_hash; # if you do not use a third-party cache management tool in your system, it is recommended to use this method: server 192.168.1.210 weight=5; server 80 weight=3; server 192.168.1.211 weight=5; server 80 weight=3; server 192.168.1.212 ip_hash; 80} 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 of grayscale publishing is realized through weight, but this method is only suitable for modifying the behavior of nodes, and requires that the applications are exactly the same. its essential function is to adjust the load capacity after nodes are added or deleted, and the ultimate goal is to keep the traffic balanced.

Method two. Using nginx+lua to realize the grayscale release 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;}

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

Method three. Use http header information to determine + weight (grayscale value)

During the transmission of http request, information such as user-agent,host,referer,cookie will be automatically brought with it. We only need to determine the information in the ip address field, user agent, cookie, etc. Let's take cookie as an example.

Of course, there are two problems that need to be solved:

① may not generate cookie when visiting static pages for the first time

②, we need to dynamically set the route through the code.

③ controls the grayscale value through weight

We can use an example to solve the problem of ② and ③ mentioned above.

Upstream tts_v6 {server 192.168.3.81 upstream tts_v7 {server 192.168.3.81 upstream tts_v6 5380 max_fails=1 fail_timeout=60;} upstream default {# through upstream default + weight node control weight server 192.168.3.81 max_fails=1 fail_timeout=60 weight=1; max_fails=1 fail_timeout=60 weight=1; 192.168.3.81 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 route 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 the question ①, we can access the dynamic page through script on the index page:

Such as

In addition, we need to judge and generate cookie in cookieinfo.php

This is the end of the content of "what are the methods for Nginx to achieve grayscale publishing". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report