Skip to content

Instantly share code, notes, and snippets.

@tunglam14
Last active December 13, 2020 16:52
Show Gist options
  • Save tunglam14/af0e5d40206eaac633fa8fb7179b80e1 to your computer and use it in GitHub Desktop.
Save tunglam14/af0e5d40206eaac633fa8fb7179b80e1 to your computer and use it in GitHub Desktop.
LB with nginx note

Default LB

upstream foo_api {
        server 0.0.0.0:8001;
        server 0.0.0.0:8000;
}

Requests are balanced between 2 backends - round-robin;

option: weight

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

option: backup

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

proxy by request method

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;
        }
}

mix

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment