Several ways for Nginx to achieve load balancing

To understand load balancing, you must first understand forward proxy and reverse proxy.

Forward proxy and reverse proxy [Summary]

Note:

  • Forward proxy, the proxy is the user.
  • Reverse proxy, the proxy is the server

What is load balancing

When the number of visits per unit time of a server is greater, the pressure on the server is greater, and when it exceeds its own capacity, the server will crash. In order to avoid server crashes and give users a better experience, we share the server pressure through load balancing.

We can build many, many servers to form a server cluster. When a user accesses a website, they first visit an intermediate server, and let the intermediate server select a server with less stress in the server cluster, and then introduce the access request to this server. In this way, every user access will ensure that the pressure of each server in the server cluster tends to be balanced, sharing the server pressure and avoiding the situation of server crash.

Load balancing is implemented using the principle of reverse proxy.

Several common ways of load balancing

1. Polling (default) 
Each request is allocated to different back-end servers one by one in chronological order . If the back-end server is down, it can be automatically eliminated.

2. Weight 
specifies the polling probability, which is proportional to the access ratio, and is used when the performance of the back-end server is uneven 
.

The higher the weight, the greater the probability of being accessed, as in the above example, 30% and 70%, respectively.

3. There is a problem with the above method, that is, in the load balancing system, if a user logs in on a certain server, when the user requests for the second time, because we are a load balancing system, each request will be relocated to If a user who has logged in to a certain server is relocated to another server, his login information will be lost, which is obviously inappropriate.

We can use the ip_hash instruction to solve this problem. If the client has already accessed a server, when the user accesses it again, the request will be automatically located to the server through the hash algorithm.

Each request is allocated according to the hash result of the access ip, so that each visitor can access a back-end server fixedly, which can solve the problem of session.

4. Fair (third party) 
allocates requests according to the response time of the back-end server, and priority is given to those with short response time.

5. url_hash (third party) 
allocates requests according to the hash result of accessing the url, so that each url is directed to the same back-end server, which is more effective when the back-end server is cached.

The status of each device is set to:

1.down means that the server before the order does not participate in the load temporarily. 
2.weight defaults to 1. The larger the weight, the greater the weight of the load. 
3. max_fails: The default number of allowable request failures is 1. When the maximum number of times is exceeded, proxy_next_upstreamthe error defined by the module  will be returned.
4. fail_timeout:max_failsAfter the failure, the pause time. 

5.backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the lightest pressure



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324858972&siteId=291194637