NGINX Load Balancing
I. Load Balancing Methods
1. Round Robin
upstream test_up { server localhost:8080; server localhost:9090; server localhost:9090;}server { listen 80; server_name test; location /test.html { proxy_pass http://test_up; }}
When you enter http: // test/test.html in the address bar of the browser, nginx accesses the backend server in sequence as configured in test_up.
2. Weight Distribution
upstream test_up { server localhost:8080 weight=1; server localhost:9090 weight=2;}server { listen 80; server_name test; location /test.html { proxy_pass http://test_up; }}
Based on the distribution of weights, NGINX dynamically allocates frontend requests to backend servers.
3. ip hash allocation
upstream test_up { server localhost:8080 weight=1; server localhost:9090 weight=2; ip_hash;}server { listen 80; server_name test; location /test.html { proxy_pass http://test_up }}
The ip hash method is to HASH client requests to backend Servers Based on the Client IP address.
Note: although the weight is configured after each server in the code above, the weight does not take effect after ip hash is adopted.
2. Control the SERVER
1. down indicates that the server before a ticket is not involved in the load
2. The default weight value is 1. The larger the weight value, the larger the load weight.
3. max_fails: the default number of failed requests is 1. If the maximum number of failed requests is exceeded, an error defined by the proxy_next_upstream module is returned.
4. fail_timeout: The pause time after max_fails fails.
5. backup: Requests the backup machine when all other non-backup machines are down or busy. Therefore, this machine is under the least pressure.
Nginx supports setting multiple groups of server Load balancer instances for unused servers.
Client_body_in_file_only is set to On. You can use the client post data record in the file for debugging.
Client_body_temp_path: Set the directory of the record file to a maximum of three levels.
Location matches the URL. You can perform redirection or perform new proxy load balancing.