nginx load balance

din /etc/nginx/nginx.conf  

http { upstream backend { server backend1.example.com; server backend2.example.com; server 192.0.0.1 backup; } server { location / { proxy_pass http://backend; } } }


by deault it will use round robin to distribute the request from client to bandend server. it can also use
Least Connections – A request is sent to the server with the least number of active connections, again with server weights taken into consideration:
IP Hash – The server to which a request is sent is determined from the client IP address. In this case, either the first three octets of the IPv4 address or the whole IPv6 address are used to calculate the hash value. The method guarantees that requests from the same address get to the same server unless it is not available.
Generic Hash – The server to which a request is sent is determined from a user‑defined key which can be a text string, variable, or a combination. For example, the key may be a paired source IP address and port, or a URI as in this example:
Random – Each request will be passed to a randomly selected server.
Least Time (NGINX Plus only) – For each request, NGINX Plus selects the server with the lowest average latency and the lowest number of active connections, where the lowest average latency is calculated based on which of the following parameters to the least_time directive is included:
  • header – Time to receive the first byte from the server
  • last_byte – Time to receive the full response from the server
  • last_byte inflight – Time to receive the full response from the server, taking into account incomplete requests

猜你喜欢

转载自www.cnblogs.com/anyu686/p/13377548.html