Home >Operation and Maintenance >Nginx >How does nginx achieve load balancing?
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.
#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; } } }
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!