When you only have one server, life is grand, and things are snappy. Until the day comes where the number of users you have outstrips your CPU, database, cloud provider, or Bendix G15 cluster.
“Add more servers” you exclaim gleefully, but not so fast. There are a few considerations to take into account if you want to roll your own balancer on NGINX rather then springing for some of Cloudflare’s really cool options.
The first directive you need to know about is upstream. Upstream is the heart of your load balancing solution. The first configuration choice to make is configuring it as ip_hash or least_conn. Backup is optional, but its a great solution if you have web servers in different regions or you want to be able to take one group of servers offline at a time,
ip_hash is a great choice if you have web applications which need to hold on to the connected users data, and perhaps you don’t have a message broker or shared cookie context in place.
least_conn is a great choice if your application is stateless or you are storing all of your user data in a database. You can think of it as every incoming connection moves to the least loaded server.
Backup is a good to have, but not required. This is usually applied when you have a different regions that you want to take down, and will only become effective if the non-backup servers aren’t reachable.
Keepalive is always good to set, but definitely take a look at the connections parameter as well so you don’t overwhelm your upstreams.
Config Example:
upstream your-cool-upstreams
{
#ip_hash;
least_conn;
#Main Server 1
server 192.168.1.1:5000 fail_timeout=60s;
#Main Server 2
server 192.168.1.2:5000 fail_timeout=60s;
# Alt IPS
server 192.168.1.3:5000 fail_timeout=60s backup;
server 192.168.1.4:5000 fail_timeout=60s backup;
#
keepalive 32;
}
In the location block, the most important field you’ll be dealing with is proxy_next_upstream. proxy_next_upstream defines the conditions under which another server is tried if the first on is unavailable. The full list of config options is here, but trust us, you won’t want to leave home without configuring this one.
Location Block Example:
location / {
proxy_pass https://your-cool-upstreams;
proxy_next_upstream error timeout invalid_header;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
That is pretty much all our go-to config for nginx upstreams! I hope that this config guide gave you a little bit of an introduction on how it works. At Allwire, we typically start with an nginx balancer before suggesting more expensive options to our clients.
Shamless Plug: Have an upstream that is more often a downstream? Call in the experts, lets chat!