Skip to content

Reverse proxy and load balancer

Load balancer

  • Console (HTTPS): layer 7 is fine. It must pass WebSocket upgrades (/shell/*/ws) and set X-Forwarded-For. List the load balancer addresses in LIMDM_TRUSTED_PROXIES (comma separated addresses or networks); only then does the server take the client address from the header (rate limits, console network lists, audit). Without it every request seems to come from the load balancer. A proxy on loopback is always trusted.
  • Agent API (8444): TCP passthrough only. Mutual TLS ends at the instance, which needs the device certificate; a load balancer that terminates TLS breaks every device. The agent certificate of the server is issued by the device CA for the host of LIMDM_AGENT_URL and LIMDM_AGENT_HOSTS, the same on every instance.
  • Health checks: GET /readyz on the console port. Route traffic for both ports by the console port's readiness (HAProxy check port, a shared health check in cloud load balancers).
  • Client addresses on 8444: with plain TCP passthrough the instance sees the load balancer's address: the device's public address and location and the enrollment failure limit then see the balancer. Enable PROXY protocol version 2 on the balancer (HAProxy send-proxy-v2, AWS NLB proxy protocol v2, nginx stream proxy_protocol on) and list its addresses under Settings, Operations, PROXY protocol sources (LIMDM_AGENT_PROXY_PROTOCOL is the initial value): connections from those addresses must start with the header, connections from anywhere else must not and keep their own address. Alternatively use a balancer that keeps the client address (HAProxy usesrc clientip, LVS direct routing). Enrollment failures are limited per address and token (20 tries of one wrong token in five minutes) and per address (200 failures), per instance, so a few wrong tokens behind one balancer do not block other enrollments.

Health endpoints

Path Meaning Answer
/healthz liveness: the process serves requests; no database check, so a database outage does not restart every instance 200 ok
/readyz readiness: database reachable, it holds the schema of this version, keys loaded, the audit key is current (not outdated by a command key rotation), notifications from other instances arrive, not shutting down 200 ready or 503 with the reasons

Both are reachable without sign in and regardless of the console network lists.

Caddy

Caddy as layer 7 load balancer for the console and layer 4 for the agent port (the layer4 app needs a Caddy build with caddy-l4). Its TCP proxy cannot check /readyz on the console port, so a draining instance still gets new device connections; HAProxy handles 8444 better.

{
    layer4 {
        :8444 {
            route {
                proxy 10.0.1.11:8444 10.0.1.12:8444
            }
        }
    }
}

mdm.example.com {
    reverse_proxy https://10.0.1.11:8443 https://10.0.1.12:8443 {
        lb_policy least_conn
        health_uri /readyz
        health_interval 5s
        transport http {
            tls_server_name mdm.example.com
        }
    }
}

Instances: LIMDM_LISTEN=:8443, LIMDM_TLS_CERT/LIMDM_TLS_KEY (a certificate for mdm.example.com, or an internal one with tls_trust_pool), LIMDM_TRUSTED_PROXIES=<Caddy address>. Caddy forwards WebSockets and sets X-Forwarded-For by default.

nginx

stream {
    upstream limdm_agent {
        server 10.0.1.11:8444 max_fails=2 fail_timeout=10s;
        server 10.0.1.12:8444 max_fails=2 fail_timeout=10s;
    }
    server {
        listen 8444;
        proxy_pass limdm_agent;       # TCP passthrough, no ssl here
        proxy_timeout 1h;             # remote shell streams
    }
}

http {
    upstream limdm_console {
        least_conn;
        server 10.0.1.11:8443 max_fails=2 fail_timeout=10s;
        server 10.0.1.12:8443 max_fails=2 fail_timeout=10s;
    }
    map $http_upgrade $connection_upgrade { default upgrade; '' close; }
    server {
        listen 443 ssl;
        server_name mdm.example.com;
        ssl_certificate     /etc/ssl/mdm.example.com.pem;
        ssl_certificate_key /etc/ssl/mdm.example.com.key;
        client_max_body_size 2m;
        location / {
            proxy_pass https://limdm_console;
            proxy_ssl_server_name on;
            proxy_ssl_name mdm.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_read_timeout 1h;
        }
    }
}

Open source nginx has no active health checks; it takes an instance out after failed requests (max_fails). Instances drain for LIMDM_DRAIN_SECONDS before closing, so set it higher than fail_timeout, or use nginx Plus health_check uri=/readyz.

HAProxy

frontend console
    mode http
    bind :443 ssl crt /etc/haproxy/mdm.example.com.pem
    option forwardfor
    timeout client 1h                 # WebSockets of the remote shell
    default_backend limdm_console

backend limdm_console
    mode http
    balance leastconn
    timeout server 1h
    option httpchk GET /readyz
    http-check expect status 200
    server s1 10.0.1.11:8443 ssl verify required ca-file /etc/haproxy/ca.pem sni str(mdm.example.com) check inter 3s
    server s2 10.0.1.12:8443 ssl verify required ca-file /etc/haproxy/ca.pem sni str(mdm.example.com) check inter 3s

frontend agent
    mode tcp
    bind :8444
    timeout client 1h
    default_backend limdm_agent

backend limdm_agent
    mode tcp                          # TLS passthrough: mTLS ends at the instance
    balance leastconn
    timeout server 1h
    option httpchk GET /readyz        # health of the instance, via the console port
    http-check expect status 200
    server s1 10.0.1.11:8444 check port 8443 check-ssl verify none inter 3s
    server s2 10.0.1.12:8444 check port 8443 check-ssl verify none inter 3s

verify none on the health check only: the check reaches the console port by address. Add source 0.0.0.0 usesrc clientip (transparent proxy, needs routing back through HAProxy) to keep device addresses on 8444.