Introduced
Load balancing is a common technique for optimizing resource utilization across multiple application instances, maximizing throughput, reducing latency, and ensuring fault tolerance.
Nginx supports the following three types of algorithms:
Round-robin: Request loop release to each machine
Least connected: The next request will be sent to the server with the least number of active connections
Session Persistence: The hash function is used to determine what server should be selected for the next request (based on the IP address of the client).
1. * * Default load algorithm round-robin**
HTTP {... upstream web1 {server 192.168.1.128:9200;server 192.168.1.128:3000; } server {Listen 80; server_name localhost;location/{proxy_pass http://web1;} }}
There are two applications in the following polygon instance, 9200 ports and 3000 ports, if the weight is not specified by default polling. Proxy all HTTP requests to Web1
Nginx's reverse proxy includes: HTTP load balancer, https,fastcgi,uwsgi,scgi,memcached
If you want to give HTTPS load balancing and proxy, as long as the use of "https" as a protocol can be.
Example:
Proxy_pass Https://web1;
When you set load balancing for fastcgi,uwsgi,scgi or memcached, you also need to use the Fastcgi_pass,uwsgi_pass,scgi_pass and memcached_pass instructions separately.
2. * * Minimum connection number algorithm Least connected * *
If you want to more balance the load of each application, the least-connection algorithm should be applied for a longer connection and request time.
With the least number of connection load algorithms, Nginx will attempt to avoid excessive requests from a single application causing the load, sending new requests to the machines with fewer requests.
To use the least connection count load algorithm, you can configure the following:
Upstream Web1 {least_conn;server 192.168.1.128:9200;server 192.168.1.128:3000; }
3. * * Session hold Sessions persistence**
You will find that the "Round-robin" and "Least connected" after the two algorithms of the request can be distributed to the nodes, so there is no guarantee that the user always request to the specified server. If you now each user access will be in the Nginx service to save a session, so you need the user after the access to save the session of the machine, so that the client needs to create a "sticky" and "persistent", where we need to use IP hash algorithm to implement the load.
With Ip-hash, the IP address of the client is used as the hash key to determine what server to choose on the client's request. This method ensures that requests from the same client will always be directed except when the same server is unavailable.
To use the persistent load that holds the session, you need to set it up:
Upstream Web1 {ip_hash; Server 192.168.1.128:9200; Server 192.168.1.128:3000;}
4. * * Weight * *
We can also use weights to affect the load of server nodes, the previous example is not specified, so the weights are the same,
When using the "Round-robin" algorithm, the application is more or less, so that enough requests can be made, and when the request is processed in a uniform manner, it is done fast enough.
High-weighted node allocation requests will be more than the lower nodes, for example:
Upstream Web1 {server 192.168.1.128:9200 weight=3; Server 192.168.1.128:3000;}
4 requests will have three requests to "9200" ports, 1 to "3000" ports
It is also possible to use the latest version of Nginx in the "Least Connected" and "Ip-hash" load Algorithms
5. * * Health Check * *
Nginx's health check is for a service that fails to detect a failure and no longer has a new request. Turn off health Check when Max_fails is set to 0
Nginx supports the following health parameters:
max_fails=3 Check to fail 3 times, fail to determine
Wait 2 seconds after fail_timeout=2 service failed to retry this period service is considered unavailable and defaults to 10 seconds
6. * * Expand * *
Down flag server for downtime
Backup flag This server as a back up server when the primary server is unavailable is to request this server
Example:
Upstream Web1 {server 192.168.1.128:9200 weight=3; Server 192.168.1.129:9200 backup; Server 192.168.1.128:3000; KeepAlive 32;}
KeepAlive 32; Number of connections maintained 32
max_conns=333: Limit the maximum number of connections 333
Resolver specifying DNS resolution domain names
HTTP {resolver 10.0.0.1; Upstream U {zone ...; ... server example.com Resolve; }}
The SLOW_START=10 setting service will restore the weight 0 to normal within the specified 10 seconds.
Nginx's Upstream module
Http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
[First chapter Nginx installation basic boot and process signal]
[02-nginx IO Model]
[03-nginx Load Balancer]
This article from "Nginxs Small white" blog, reproduced please contact the author!
03-nginx Load Balancing