Home>Article>Operation and Maintenance> How to explain the nginx load balancing principle orally
Load balancing principle
The client sends a request to the reverse proxy, and then the reverse proxy forwards the request according to a certain load mechanism to the target server (these servers are all running the same application) and return the obtained content to the client. During the period, the proxy request may be sent to different servers according to the configuration. (Recommended learning:nginx tutorial)
Load balancing configuration
Test case:
As shown below, the same application is deployed on two servers (192.168.1.103, 192.168.1.102), and the website is accessed through port 8080, as follows
http://192.168.1.xx:8080/webautotest/xxxxxxx
At the same time on 192.168. The nginx reverse proxy is installed on 1.103, and I want to access the two sites through port 80 of 192.168.1.103
Edit the nginx configuration file (in the example: /usr/local/ngnix/conf/nginx. conf), find the http node, as follows, add the content with the background color
##
http { upstream myapp1 { server 192.168.1.103:8080; server 192.168.1.104:8080; } ……略 server { listen 80; server_name localhost; ……略 location /webautotest/ { proxy_buffering off; proxy_pass http://myapp1; } } }
Reload the configuration file
[root@localhost nginx-1.10.0]# /usr/local/ngnix/sbin/nginx -s reload
Access the test url
As follows, access the same page and display pages from different serversDescription:
Load balancing method
nginx provides the following three load balancing mechanisms, Method: round-robin — Requests are distributed to the application server in a round-robin 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; } } }In the above example, there are three application instances running on srv1-srv3 respectively. When the specified load balancing method is not displayed, the default is round-robin. All requests are forwarded by the proxy to the myapp1 server group and the requests are distributed according to the load balancing method.
The above is the detailed content of How to explain the nginx load balancing principle orally. For more information, please follow other related articles on the PHP Chinese website!