Linux

根據流量或請求的百分比對 HTTP 進行負載平衡?

  • April 1, 2022

我想將我的應用程序的 v1 放在一個池中,將 v1.1 版本放在另一個池中,然後慢慢增加流向池 2 的流量並將其減少到池 1。

誰能展示一些使用 HA Proxy、Varnish、Nginx 或其他東西做這件事的具體例子?

拆分客戶端模組專門為此設計:

# I like starting my upstream names with _
# so they're not confused with hostnames
upstream _old_upstream {
 server 127.0.0.1:1234;
}

upstream _new_upstream {
 server 127.0.0.1:4321;
}

# Make sure the values here match the names of the upstream blocks above
split_clients $remote_addr $split_upstream {
  10% _new_upstream;
  -   _old_upstream;
}

server {
 location / {
   # EDIT: I forgot, when using variables in a proxy_pass, you have to
   # specify the entire request
   proxy_pass http://$split_upstream$request_uri;
 }
}

然後,當您想將更多流量轉移到新伺服器時,只需更改百分比並執行nginx -s reload.

引用自:https://serverfault.com/questions/384956