Nginx can be configured to proxy multiple servers. When a server goes down. The system remains available. Let’s talk about some commonly used configuration items.
upstream configuration:
Just add upstream configuration under http configuration:
upstream nodes { server 192.168.10.1:8668; server 192.168.10.2:8668; }
upstream makes requests to the configured upstream server according to the default polling method. If the upstream server hangs up, it can be automatically removed without manual intervention. This method is simple and quick. But if the upstream server configuration is imbalanced, it cannot be solved. So nginx has many other configuration items. Let’s introduce them one by one.
Weight configuration:
Weight is proportional to the number of requests and is mainly used when the upstream server configuration is unbalanced. In the configuration below, the request volume of the 192.168.10.2 machine is twice the request volume of the 192.168.10.1 machine.
upstream nodes { server 192.168.10.1:8668 weight=5; server 192.168.10.2:8668 weight=10; }
ip_hash configuration:
Each request is allocated according to the hash result of the requested IP. In this way, each request is fixed on an upstream server, which can solve the problem of IP sessions on the same server.
upstream nodes { ip_hash; server 192.168.10.1:8668; server 192.168.10.2:8668; }
fair configuration:
Distribute requests according to the response time of the upstream server. Prioritize allocation with short response times.
upstream nodes { server 192.168.10.1:8668; server 192.168.10.2:8668; fair; }
url_hash configuration:
Distribute requests according to the hash result of the accessed URL, so that each URL is directed to the same upstream server. Note: Add hash statement in upstream. Other parameters such as weight cannot be written in the server statement. hash_method is the hash algorithm used.
upstream nodes { server 192.168.10.1:8668; server 192.168.10.2:8668; hash $request_uri; hash_method crc32; }
down: Indicates that the current server does not participate in load balancing.
max_fails: The number of failed requests defaults to 1.
fail_timeout: The time to pause requests to this server after max_fails failures.
backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the least pressure.
For more Nginx related technical articles, please visit the Nginx usage tutorial column to learn!
The above is the detailed content of What are the nginx load balancing parameters?. For more information, please follow other related articles on the PHP Chinese website!