nginx的负载均衡策略常用的主要有以下四种:
1、轮训(默认),nginx会按请求的时间顺序分配到172.0.0.2和172.0.0.3,如果有出现后端节点故障就会被剔除
upstream a_server{ server 172.0.0.2:8080; server 172.0.0.3:8080; } server{ listen 80; server_name 172.0.0.1; location /a/{ proxy_pass http://a_server; } }
2、权重,weight表示权重,值越大表示权重越高,被分配的请求也就越多
upstream a_server{ server 172.0.0.2:8080 weight=1; server 172.0.0.3:8080 weight=2; } server{ listen 80; server_name 172.0.0.1; location /a/{ proxy_pass http://a_server; } }
3、IP HASH,nginx会根据ip的hash结果来分配请求,每个请求会被分配到同一个后端节点,保持会话,session同步
upstream a_server{ ip_hash; server 172.0.0.2:8080; server 172.0.0.3:8080; } server{ listen 80; server_name 172.0.0.1; location /a/{ proxy_pass http://a_server; } }
4、最小连接,新的请求会被分配到当前最小连接的后端节点
upstream a_server{ least_conn; server 172.0.0.2:8080; server 172.0.0.3:8080; } server{ listen 80; server_name 172.0.0.1; location /a/{ proxy_pass http://a_server; } }