upstream foo_api {
server 0.0.0.0:8001;
server 0.0.0.0:8000;
}
Requests are balanced between 2 backends - round-robin;
upstream foo_api {
server 0.0.0.0:8001 weight=5;
server 0.0.0.0:8000; # weight=1
}
In 6 requests, each 5 requests go to 8001 1 request go to 8000
upstream foo_api {
server 0.0.0.0:8001 weight=5;
server 0.0.0.0:8000 weight=2;
}
In 7 requests, each 5 requests go to 8001 2 requests go to 8000
upstream foo_api {
server 0.0.0.0:8001 weight=5;
server 0.0.0.0:8000 backup;
}
Test command:
$ while true; do curl -I localhost >> /tmp/log; done
Result:
root@test-nginx-lb:/etc/nginx/sites-enabled# cat /tmp/log | grep 'HTTP/1.1' | sort -n | uniq -c
368 HTTP/1.1 200 OK
upstream foo_api_POST {
server 0.0.0.0:8001;
}
upstream foo_api_GET {
server 0.0.0.0:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
proxy_pass http://foo_api_$request_method;
}
}
upstream foo_api_POST {
server 0.0.0.0:8001;
server 0.0.0.0:8000 backup;
}
upstream foo_api_GET {
server 0.0.0.0:8000;
server 0.0.0.0:8001 backup;
}
or
upstream foo_api_POST {
server 0.0.0.0:8001 weight=9;
server 0.0.0.0:8002;
server 0.0.0.0:8000 backup;
}
upstream foo_api_GET {
server 0.0.0.0:8000 weight=9;
server 0.0.0.0:8002;
server 0.0.0.0:8001 backup;
}