round-robin django setup with nginx

Goal: improve site's availablity in the face of unexpected crashes as well as scheduled application updates.

Gist: use multiple copies of the [django] application proxied with nginx load-balancing mechanism.

Implementation is trivial, just take example configuration from nginx doc and customize for your setup. E.g.

upstream django-vsesto {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
}

server {
    location / {
        proxy_pass http://django-vsesto/;
        include /etc/nginx/proxy.conf;
        proxy_connect_timeout   2;
        proxy_next_upstream error timeout;
    }
}

Here is the interesting bit. Setting connect timeout to be small enough allows nginx to try another backend server to serve user's request. Which means user won't see any 502 error pages at all, if there is at least one working application server running. I'm using monit that ensures the app servers get started if they crash or hang for some reason.