SoFunction
Updated on 2025-04-11

Summary of the six ways of distribution of nginx upstream

1. Polling (default)

Each request is assigned to a different backend server in chronological order. If the backend server is down, it can be automatically eliminated.

2 weight

Specifies the polling probability, weight and access ratio, which are proportional to the backend server's performance is uneven.
For example:

upstream bakend {
         server 192.168.0.88 weight=10;
         server 192.168.0.89 weight=10;
}

3 ip_hash

Each request is assigned according to the hash result of accessing the IP, so that each visitor is fixed to access a backend server, which can solve the session problem.
For example:

upstream bakend {
         ip_hash;
         server 192.168.0.88:80;
         server 192.168.0.89:80 weight=10;
}

4 fair (third party)

Requests are allocated according to the response time of the backend server, and the response time is given priority.
For example:

upstream bakend {
         server 192.168.0.88:80;
         server 192.168.0.89:80;
         fair;
}

5 url_hash (third party)

The request is allocated according to the hash result of accessing the url, so that each url is directed to the same backend server. The backend server is more effective when it is cached, which helps to improve the cache hit rate.
For example:

upstream backend {
         server 192.168.0.88:3128;
         server 192.168.0.89:3128;
         hash $request_uri;
         hash_method crc32;
}

6 backup (Suspension) Therefore, the backup entry will take effect only if the normal node is hung up.

 upstream tomcat_upstream {
          server  192.168.0.2:8080 weight=10;
          server  192.168.0.3:8080 weight=10 backup;
    }

The status of each device is set to:

  • down means that the server before the order does not participate in the load for the time being
  • The larger the default weight, the greater the weight of the load.
  • max_fails: The number of times that allow requests fail is 1 by default. When the maximum number exceeds, return an error defined by the proxy_next_upstream module.
  • fail_timeout: the time of pause after max_fails failure.

This is the end of this article about the six ways of distribution of nginx upstream. For more related content of nginx upstream, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!