【Nginx】Nginx load balancing

1.Nginx load balancing


  • 1.2 Default method: round-robin

    round-robin — requests to the application servers are distributed in a round-robin fashion, distributed in a round-robin mode
    , written as follows:
    upstream block, defines a set of services, followed by the name of this set of services: myapp1
    does not need Special declarations default to polling mode.
    http {
          
          
    	# 一组服务
        upstream myapp1 {
          
          
            server srv1.example.com;
            server srv2.example.com;
            server srv3.example.com;
        }
    
        server {
          
          
            listen 80;
    
            location / {
          
          
                proxy_pass http://myapp1;
            }
        }
    }
    

  • 1.3 Least-connected links

    least-connected — next request is assigned to the server with the least number of active connections,
    the next request will be assigned to the server with the least number of active connections.
    The difference between this and the default writing method is that in the first line of the upstream block, the load balancing method is declared as least_conn
    upstream myapp1 {
    
    
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }
    ...

  • 1.4 Session persistence, also called ip hash (Session persistence)

    a hash-function is used to determine what server should be selected for the next request (based on the client's IP address).
    A hash method and algorithm will be used to determine which service will accept the next user request (the algorithm is based on the user IP).
    To put it simply, requests with the same IP address (which is equivalent to requests from the same user) will be distributed to the same server, so that the session related to the user information saved on the server can be reused, similar to persisting the user's session. session)
    The difference between this and the default writing method is that in the first line of the upstream block, the load balancing method is declared as ip_hash
	upstream myapp1 {
    
    
	    ip_hash;
	    server srv1.example.com;
	    server srv2.example.com;
	    server srv3.example.com;
	}
    ...

  • 1.5 Server weight (Weighted)

    This is easy to understand, that is, before adding weights, every server is treated equally, but after adding weights, servers with more weights will be reused, that is, they will be assigned more requests. In the early stage, there are enough requests.
    The writing method is also relatively simple, that is, adding a weight parameter weight = x after the server
    upstream myapp1 {
    
    
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }
    ...

As above, for every five requests, 3 will be sent to the first server, and one to each of the following two servers.

It is similarly possible to use weights with the least-connected and ip-hash load balancing in the recent versions of nginx.
Official: In the latest Nginx version, it is also possible to use weight parameters with least-connected and ip-hash.

Guess you like

Origin blog.csdn.net/cjl836735455/article/details/131487195