Analyze nginx upstream weight to 0 and delete nodes

Nginx upstream is something that people often use. The upstream method can easily implement load balancing of services. But often many people do not understand the difference between nginx upstream delete host and weight configuration 0. From the official introduction, we can only know that weight = 0 does not forward new requests, delete hosts to go offline. But you need to understand the impact of this type of operation on long connections. Because if you don't understand this, it's easy to cause service anomalies, even for a small amount of time.

The weight is set to 0: upstream resets the weight of a node to 0, nginx no longer forwards new requests to the node, the old connection hangs like this, the established connection is not interrupted, until timeout or active disconnection. New The connection will not go to the node.

Remove the host: Delete the node in the upstream list. nginx will first close the connection that is not in transit, and then wait for the data transfer to complete, then close the connection.

upstream bakend {
server {
    listen       8122 default_server;
    server_name  _;
    root         /var/lib/download;
    include /etc/nginx/default.d/*.conf;
    location / {
        proxy_pass http://bakend;

As for the port change, you can use it. In order to test the active long connection, use chunk chunking to delay the return.

package main
import (
func main() {
    http.HandleFunc("/test", HandlePost);
    fmt.Println(http.ListenAndServe(":7080", nil))
func HandlePost(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Connection", "Keep-Alive")
    w.Header().Set("Transfer-Encoding", "chunked")
    w.Header().Set("X-Content-Type-Options", "nosniff")
    ticker := time.NewTicker(time.Second)
    go func() {
        for t := range ticker.C {
            io.WriteString(w, "Chunk\n")
            fmt.Println("Tick at", t)
    time.Sleep(time.Second * 15)
    fmt.Println("Finished: should return Content-Length: 0 here")
    w.Header().Set("Content-Length", "0")
import time
import os
import requests
s = requests.Session()
print os.getpid()
res = s.get("")
print res.content
s = requests.Session()
res = s.get("")
print res.content 

In summary, whether the weight is configured to 0 and the host is removed, the safe exit of the service is guaranteed, and the elegant grayscale goes online and offline.

Omitmepty in Golang
File permission after copy a file from a account t...


No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Sunday, 16 December 2018