Platform server load balancing
If a load balancer runs in front of platform servers, passive server ejection by errors (max_fails/fail_timeout) is not enough. A node may accept a TCP connection — its nginx is alive — while the service inside is still starting or has lost Mongo/RabbitMQ. Such a node responds with 502, and with max_fails=0 (found in some configurations) the balancer never considers it down: every request keeps going to it and returns 502.
An active check solves this: the balancer polls /readiness of every node and ejects it from rotation until it recovers. The status endpoints are described in Getting HTTP service statuses.
Readiness checks
Check /<service>/readiness of the service that serves the domain:
| Domain | Service | Check |
|---|---|---|
| web interface, customer portal | webcontrol | /webcontrol/readiness |
admin panel (/admin/) | webadmin | /webadmin/readiness |
API (/api/) | webapi | /webapi/readiness |
| click tracking | trkaction | /trkaction/readiness |
| web version of tracking, pages | trkwebctrl | /trkwebctrl/readiness |
| pixels | cookiesaver | /cookiesaver/readiness |
Rules common to any load balancer:
- Use
/readiness, not/health:/healthreturns code 200 as long as the process is alive and checks nothing. - The check is performed with the
GETmethod only. - The internal check timeout on the platform side is 500 ms. Set the balancer check timeout to at least 1 s, otherwise a node will be marked unavailable by mistake under peak load.
- Create a separate upstream (backend) for each service and check the readiness of that service: a node is marked unavailable as a whole, so a
cookiesaverfailure must not take the web interface out of balancing. - Check the web interface domain: all six paths are served by its nginx. The tracking and pixel domains (separate platform nginx instances) do not have them — the request returns 404 and the node is marked unavailable.
- A frequent check loads Mongo and RabbitMQ: the service polls its subsystems on every request. A 5-second interval is enough.
- The
/…/readinesspaths require no authentication. Restrict access to them on the external proxy to the balancer addresses.
You can check the response of a particular node like this:
curl -sk -H 'Host: cdp.example.com' https://10.135.196.143/webcontrol/readiness
On problems the response has code 503 and lists the subsystems that failed the check:
{
"status": "FAIL",
"failed": ["mongo"]
}
If a service is not running on the server, nginx returns code 502 with the "The page is temporarily unavailable" page for /<service>/readiness, not a 503 with JSON. The balancer will consider such a node unavailable and take all domains of this upstream out of rotation. Before enabling a check, make sure every endpoint returns 200 on every node:
curl -s -o /dev/null -w '%{http_code}\n' http://172.16.0.10/webcontrol/readiness
On installations where some services are not running (for example, no pixels and no cookiesaver), do not enable the check for that service.
Passive checks without rebuilding
The minimal step if the balancer cannot be changed:
upstream altcraft-web {
server 172.16.0.10:80 max_fails=3 fail_timeout=30s;
server 172.16.0.11:80 max_fails=3 fail_timeout=30s;
}
server {
# ...
location / {
proxy_pass http://altcraft-web;
proxy_next_upstream error timeout http_502;
# ...
}
}
max_fails must be greater than zero (the value 0 disables passive checks), and code 502 must be added to proxy_next_upstream explicitly: by default only connection errors and timeouts are retried. Limitations: a node is ejected only after several failed requests, returns to rotation when fail_timeout expires without a readiness check, and non-idempotent requests (POST) are not retried on another node by default.
NGINX with the nginx_upstream_check_module
Open-source nginx has no active checks: they exist only in NGINX Plus. Regular nginx uses the nginx_upstream_check_module module, which requires rebuilding nginx from source with a patch for your version.
Build (the patch is chosen by nginx version, see the module README: check_1.20.1+.patch for 1.20.1+, check_1.30.4+.patch for 1.30.4+):
nginx -V # keep the configure arguments line: it must be passed to the build
cd /usr/src
curl -LO https://nginx.org/download/nginx-<version>.tar.gz
tar xzf nginx-<version>.tar.gz
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
cd nginx-<version>
patch -p1 < ../nginx_upstream_check_module/check_1.20.1+.patch
./configure <configure arguments from nginx -V> \
--add-module=../nginx_upstream_check_module
make && make install
On Debian/Ubuntu and RHEL it is safer to build from the distribution source packages (apt-get source nginx or the distribution SRPM) and add the module to their build files: otherwise the build flags, paths and package unit files are lost.
Reference topology of the examples: the balancer terminates TLS and proxies to the nodes over HTTP on port 80, and the node distributes requests by the Host header. Request headers and the connection version are set in every location: without them nginx puts the upstream name (for example, altcraft-web) into Host and does not pass X-Forwarded-*. Repeat the block when adding a new location. Checks go to the same place — with the web domain Host, because all /<service>/readiness paths exist only in the web domain nginx. The network between the balancer and the nodes must be trusted: traffic to the nodes goes without TLS.
Settings shared by all domains are in nginx.conf:
# /etc/nginx/nginx.conf — balancer in front of platform servers
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 30;
# The request body size is not limited: the application controls it.
client_max_body_size 0;
large_client_header_buffers 4 400k;
# All proxy_* directives are set in every location — see conf.d/*.conf.
# State of the checks (nginx_upstream_check_module).
check_shm_size 1m;
# State of all upstreams — locally only.
server {
listen 127.0.0.1:8080;
location = /status {
check_status json;
}
}
include /etc/nginx/conf.d/*.conf;
}
Web interface, admin panel and API — /etc/nginx/conf.d/web.conf:
# /etc/nginx/conf.d/web.conf — web interface, admin panel and API
# A separate upstream per service: a failure of one service
# does not take the node out of rotation for the others.
upstream altcraft-web {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /webcontrol/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
upstream altcraft-admin {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /webadmin/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
upstream altcraft-api {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /webapi/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
server {
listen 80;
server_name altcraft.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name altcraft.example.com;
ssl_certificate /etc/letsencrypt/live/altcraft.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/altcraft.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-web;
}
location /admin/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-admin;
}
location /api/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-api;
}
# Progress stream of bulk operations: no events while idle.
location /ajax/account_stream {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 20000;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-web;
}
}
Tracking — /etc/nginx/conf.d/tracking.conf. The domain is served by two services: trkaction handles clicks and redirects, trkwebctrl serves the web version of emails, pages and in-app:
# /etc/nginx/conf.d/tracking.conf — tracking
upstream altcraft-tracking {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /trkaction/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
upstream altcraft-tracking-web {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /trkwebctrl/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
server {
listen 80;
server_name click.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name click.example.com;
ssl_certificate /etc/letsencrypt/live/click.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/click.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
# Web version of emails, pages and in-app — trkwebctrl.
location ~ "^/(w|inapp)/" {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-tracking-web;
}
# Clicks and redirects — trkaction.
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-tracking;
}
}
Pixels, push and subscription forms — /etc/nginx/conf.d/pixel.conf:
# /etc/nginx/conf.d/pixel.conf — pixels, push and subscription forms
upstream altcraft-pixel {
server 172.16.0.10:80;
server 172.16.0.11:80;
check interval=5000 rise=2 fall=3 timeout=2000 type=http;
check_http_send "GET /cookiesaver/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
server {
listen 80;
server_name pxl.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name pxl.example.com;
ssl_certificate /etc/letsencrypt/live/pxl.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/pxl.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1h;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffer_size 64k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 64k;
proxy_read_timeout 900;
proxy_next_upstream error timeout http_502;
proxy_pass http://altcraft-pixel;
}
}
Some paths of the pixel domain (/form, /event, /popup, /inapp) are served by trkwebctrl. There is no separate upstream for them in the example: the domain is checked by cookiesaver, and when it fails the node is taken out of rotation as a whole — for pixels this is safer than a 502 on every request.
If the nodes are not reachable from the balancer over HTTP at all (they accept HTTPS only), bring up an auxiliary HTTP port for checks on each node. The change is needed in every domain file — the example shows web.conf:
# On the platform node
server {
listen 10.0.0.10:8080;
server_name _;
allow 172.16.0.200; # balancer address
deny all;
location / {
proxy_pass http://127.0.0.1:80;
}
}
# On the balancer: servers stay on 443, the check goes to 8080
upstream altcraft-web {
server 10.0.0.10:443;
server 10.0.0.11:443;
check interval=5000 rise=2 fall=3 timeout=2000 type=http port=8080;
check_http_send "GET /webcontrol/readiness HTTP/1.0\r\nHost: altcraft.example.com\r\n\r\n";
check_http_expect_alive http_2xx;
}
Then the domain server blocks also proxy over HTTPS: proxy_pass https://altcraft-web;, proxy_ssl_server_name on; and proxy_ssl_verify off; for self-signed node certificates (or proxy_ssl_trusted_certificate with their CA).
Check parameters:
interval=5000— poll every 5 seconds. Do not decrease it: every/readinessrequest makes the service poll Mongo and RabbitMQ.fall=3,rise=2— the node is ejected after three failed checks (≈15 s) and returns after two successful ones (≈10 s).timeout=2000— the platform responds to/readinesswithin 500 ms, but a margin is needed under load; a timeout below 1 s causes false positives.check_http_send— the whole request, includingHost. nginx variables ($host) are not expanded in this directive, the value must be literal.HTTP/1.0is preferable: the connection is closed after the response and the check does not wait for the timeout.check_http_expect_alive http_2xx— on problems the platform responds with 503 and the node is marked unavailable.
Module limitations:
type=httpchecks over plain HTTP — it does not support TLS. For HTTPS nodes useport=(see above) or HAProxy.type=tcpandtype=ssl_hellocheck only port availability and the TLS handshake, they do not see service readiness — they are not suitable for balancing.- By default (
default_down=true) a server is considered unavailable until the first successful check. A mistake incheck_http_send(for example, a missingHostheader) will mark all nodes unavailable.
Server state: curl http://127.0.0.1:8080/status?format=json. In the response status is up/down, rise/fall are the counters of successful and failed checks:
{
"servers": {
"total": 2,
"generation": 3,
"server": [
{ "index": 0, "upstream": "altcraft-web", "name": "172.16.0.10:80", "status": "up", "rise": 58, "fall": 0, "type": "http", "port": 80 },
{ "index": 1, "upstream": "altcraft-web", "name": "172.16.0.11:80", "status": "down", "rise": 0, "fall": 3, "type": "http", "port": 80 }
]
}
}
HAProxy
HAProxy checks HTTP endpoints out of the box, without modules or rebuilding, and supports HTTPS checks — this is simpler when nodes terminate TLS themselves or certificates are self-signed. A complete configuration:
# /etc/haproxy/haproxy.cfg — balancer in front of platform servers.
# Domain certificates: /etc/haproxy/certs/<domain>.pem (certificate + key).
global
log /dev/log local0
maxconn 20000
stats socket /var/run/haproxy.sock mode 660 level admin
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 10s
timeout client 900s
timeout server 20000s # inactivity: covers the /ajax/account_stream stream
timeout check 2s
retries 2
option redispatch
frontend fe_http
bind *:80
http-request redirect scheme https code 301
frontend fe_https
bind *:443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
acl host_web hdr(host) -i altcraft.example.com
acl host_trk hdr(host) -i click.example.com
acl host_pxl hdr(host) -i pxl.example.com
acl path_admin path_beg /admin/
acl path_api path_beg /api/
acl path_trk_web path_beg /w/ /inapp/
# A separate backend per service: a failure of one service
# does not take the node out of rotation for the others.
use_backend be_admin if host_web path_admin
use_backend be_api if host_web path_api
use_backend be_web if host_web
use_backend be_trk_web if host_trk path_trk_web
use_backend be_trk if host_trk
use_backend be_pxl if host_pxl
default_backend be_web
backend be_web
balance roundrobin
option httpchk
http-check send meth GET uri /webcontrol/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
backend be_admin
balance roundrobin
option httpchk
http-check send meth GET uri /webadmin/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
backend be_api
balance roundrobin
option httpchk
http-check send meth GET uri /webapi/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
backend be_trk
balance roundrobin
option httpchk
# The check uses the web domain Host/SNI: all /<service>/readiness paths
# are served by the web domain nginx, not by the tracking domain one.
http-check send meth GET uri /trkaction/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
backend be_trk_web
balance roundrobin
option httpchk
http-check send meth GET uri /trkwebctrl/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
backend be_pxl
balance roundrobin
option httpchk
http-check send meth GET uri /cookiesaver/readiness ver HTTP/1.1 hdr Host altcraft.example.com hdr Connection close
http-check expect status 200
default-server check inter 5s fall 3 rise 2 ssl verify none check-sni altcraft.example.com
server node1 10.0.0.10:443
server node2 10.0.0.11:443
Notes:
http-check senddefines the whole request: path, version and the web domainHostheader.option httpchkwithout arguments enables checks, and the request itself is described byhttp-check send(HAProxy 2.2+).http-check expect status 200— a 503 from/readinessputs the server inDOWN;option redispatchandretriesforward the request to another node. To check the response body too, addhttp-check expect string "\"status\":\"OK\"".ssl verify noneis needed for self-signed node certificates. If the certificates are valid, usessl verify required ca-file /etc/haproxy/ca.pem.check-sni(HAProxy 2.1+) is mandatory when one address serves several domains: otherwise the check hits the wrong nginxserverblock. The value is the web domain, the same as inHost.timeout serveris the inactivity timeout. The value of 20000 s covers the progress stream of bulk operations (/ajax/account_stream); if such a timeout is undesirable for other requests, move the stream to a separate backend with its owntimeout.- HAProxy has no request body size limit like
client_max_body_sizein nginx. - The check goes to 443 with the web domain
Hostand SNI, so every node must serve the web domain: in a homogeneous platform cluster this is the case. - The web version of emails, pages and in-app (
/w/,/inapp/) are moved to a separatebe_trk_webbackend: they are served bytrkwebctrl, so it has its own check.
Server state:
echo "show servers state be_web" | socat stdio /var/run/haproxy.sock
The external nginx in front of the platform can be kept as a TLS terminator with HAProxy doing the balancing, or nginx can be replaced by HAProxy entirely.
Verification after setup
- Make sure every node responds to the check with code 200 (see Readiness checks).
- Open the balancer status page: all nodes must be
up. - Stop a service on one node (
./akd stopor disable Mongo on it) and make sure the node goesdownwhile requests are still served by the remaining nodes. - Make sure the node returns to rotation after recovery, and that file uploads and the progress stream of bulk operations (a long request without a response) are not cut off by a timeout.