Home > Operation and Maintenance > Nginx > How does nginx achieve load balancing?

How does nginx achieve load balancing?

(*-*)浩
Release: 2019-11-01 10:25:17
Original
5670 people have browsed it

Nginx ("engine x") is a high-performance Web and reverse proxy server developed by Russian programmer Igor Sysoev. It is also an IMAP/POP3/SMTP proxy server.

How does nginx achieve load balancing?

#In the case of high connection concurrency, Nginx is a good alternative to the Apache server.

Load balancing: Load balancing is also a commonly used function of Nginx. When the number of visits per unit time of a server is greater, the pressure on the server will be greater, to the point that it exceeds its own endurance. ability, the server will crash. In order to avoid server crashes and provide users with a better experience, we use load balancing to share server pressure. (Recommended learning: nginx tutorial)

We can build many, many servers to form a server cluster. When a user accesses the website, he first accesses an intermediate server, and then lets this intermediate server run on the server Select a server with less pressure in the cluster, and then introduce the access request to that server.

Since then, every time a user visits, it will ensure that the pressure of each server in the server cluster tends to be balanced, sharing the server pressure and avoiding server crash. Load balancing configuration generally requires configuring a reverse proxy at the same time, and jumping to load balancing through the reverse proxy.

nginx provides the following three load balancing mechanisms and methods:

round-robin - requests are distributed to application servers in a circular and rotating manner.

least-connected — The next request is assigned to the server with the least number of active connections

ip-hash — Uses a hash function to determine where the next request should be based on the client IP address Which server to distribute to.

Default load balancing configuration

http {
   upstream myapp1 {
       server srv1.example.com;
       server srv2.example.com;
       server srv3.example.com;
   }
  
   server {
       listen 80;
  
       location / {
           proxy_pass http://myapp1;
       }
   }
}
Copy after login

The above is the detailed content of How does nginx achieve load balancing?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template