1.轮询(默认)
将请求按顺序轮流发送到相应的服务器
http {
upstream cluster {
server 192.168.56.1:8080;
server 192.168.56.2:8080;
server 192.168.56.3:8080;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}
}
2.最少连接数
将请求发送到当前连接数最少的服务器。
http {
upstream cluster {
least_conn;
server 192.168.56.1:8080;
server 192.168.56.2:8080;
server 192.168.56.3:8080;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}
}
3.权重
根据权重将请求发送到相应的服务器,权重高的服务器会受到更多的请求。
http {
upstream cluster {
server 192.168.56.1:8080 weight=3;
server 192.168.56.2:8080 weight=2;
server 192.168.56.3:8080 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}
}
4.ip-hash
根据请求ip的hash结果将请求发送的相应的服务器,保证同一个ip始终发送到同一个服务器。
http {
upstream cluster {
ip_hash;
server 192.168.56.1:8080;
server 192.168.56.2:8080;
server 192.168.56.3:8080;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}
}
5.fair(第三方)
将请求发送到响应时间短的服务器上。
http {
upstream cluster {
server 192.168.56.1:8080;
server 192.168.56.2:8080;
server 192.168.56.3:8080;
fair;
}
server {
listen 80;
location / {
proxy_pass http://cluster;
}
}
}