Related posts
- Removing HTTP/3 from nginx cost nothing
My setup script now builds nginx without HTTP/3, because every site sits behind Cloudflare, which talks to the origin over TCP anyway. The A/B against the previous release came out flat on every XenForo tier, and a slow drift in the test data was a reminder to only compare arms run side by side.
- Whitespace is cheap: the nginx trim filter lost, and five setup options became automatic
I built Tengine's HTML trim filter into my compiled nginx and measured it on a seeded XenForo forum. It made member pages 7-10 % smaller and every PHP-rendered page 1.6-3 % slower, so it did not ship. The same release of my setup script turns five command-line options into values derived from the hardware, shows what --optimize is compiling and why, closes a root chown race in a nightly cron job, and tightens the XenForo guest cache.
- Compiling Valkey, and the BOLT flag that gave my binaries an executable stack
Valkey from source was 35 % slower until I stopped passing CFLAGS on make's command line. Then its unit refused the BOLTed binary, which exposed an executable stack in my compiled PHP and Angie too. Plus nginx vs Angie, compiled the same way.
My setup script compiles almost everything it installs. Release 1.22.48 adds a deliberate exception:
setup.sh --centrifugoinstalls Centrifugo v6, the realtime server behind my XenForo add-on’s live features (new posts appearing in a thread, typing indicators, presence, conversation pushes). This post covers what the command sets up, the load test that decided its defaults, and the bugs the first live run found.Why a release binary and not a source build#
Centrifugo is a static Go binary. Everything else the script compiles is C or C++, where my build flags, LTO and BOLT make a measured difference. A Go build would need its own toolchain and its own PGO pipeline, and I have no number showing it would pay. My rule for a new default is at least 2 % faster somewhere and slower nowhere, so a source build comes back only if an A/B shows that much.
Taking someone else’s binary means verifying it properly, and the check fails closed:
checksums.txtand the digest GitHub recorded for the asset, andchecksums.txtmust match GitHub’s digest of it.centrifugomember is extracted, and it must be a regular file that reports the expected version.version=latestfollows the newest 6.x, and a v5 or v7 is refused.A new binary goes live only after it accepts the live configuration (
centrifugo checkconfig) and answers/healthafter its restart. Otherwise the previous binary comes back.Loopback only, in a tight unit#
Centrifugo listens on loopback only: port 8000 for the client WebSocket, port 8001 for the HTTP API and
/health. No firewall rule is added and no new public port opens. After every start the script checks the process’s listeners, and if it is bound anywhere else, the unit is stopped and the step fails. That includes a listener an operator override would have added.The systemd unit runs it as its own system user with no capabilities,
ProtectSystem=strictand no writable path (logs go to the journal).IPAddressDeny=anywith only localhost allowed gives it no egress at all.NoExecPaths=/with only the binaries’ directory executable, a syscall filter,MemoryDenyWriteExecute,PrivateUsersandProtectProc=invisiblefinish it.systemd-analyze securityrates it 1.1 (“OK”) on the Xeon E-2236 test host.One hardening option is deliberately missing:
ProcSubset=pid. Go sizes its listen backlog from/proc/sys/net/core/somaxconn, and without that file it quietly falls back to 128. On a forum where thousands of tabs reconnect at once after a restart, a backlog of 128 is a real limit.The unit has no
ExecReload. The script reloads withsystemctl kill --signal=HUPaimed at the main process, which systemd delivers itself. If the signal can’t be delivered, the change becomes one restart, never a reload that is reported but didn’t happen.Secrets never touch the command line#
The token secret and the API key are generated once with
openssl rand -hex 48into a root-only file. The script parses that file and never sources it. It takes no secret as an option:secret=andkey=are unknown options, and a mistyped one is masked in the run log. No secret is printed or put on any process’s argv; the API key reachescurlon stdin. A root-only credentials file holds what the add-on’s admin settings need, and only its path is ever printed.rotate=secretmakes a new token secret and keeps the old one valid for 24 hours and 5 minutes (the add-on’s longest token lifetime plus a margin). That is a SIGHUP, so open sockets survive.rotate=apikeycosts one restart.nginx: one exact location, small buffers#
Browsers reach Centrifugo through the forum’s own nginx, on the same origin. The script inserts one include, between markers, into the serving 443 block of each vhost whose board has the add-on. It defines a single exact location,
/connection/websocket, GET only, with no access log. SSE, the other transports,/apiand/healthare never proxied; they fall through to XenForo’s 404.The include uses a lean tunnel profile with 8k buffers instead of 128k. An upgraded connection holds one buffer per direction for its whole life, so the large buffers cost about 256 KB of nginx memory per member socket for nothing; realtime messages are small. Each changed vhost has to pass
nginx -tbefore the single reload, and every file is put back on failure.After wiring, a handshake through nginx on loopback has to give 101 for the forum’s real
Originand 403 for a foreign one. A foreign origin that upgrades fails the step.XenForo and re-runs#
The add-on reads its realtime settings from
src/config.php, so the script writes them into a managed block in each add-on board’s config.php. Every write passesphp -lfirst and keeps the file’s owner, mode and inode; removal is byte-exact. A setting the operator wrote outside the block is left alone, and the keys that differ are named, never their values. Switching realtime on in the admin panel stays manual.Like the rest of the script, a re-run changes nothing: same secrets, same bytes in every file, no restart, no reload. A change costs the cheapest action that applies it: nothing, a SIGHUP, or one restart for binary, unit and configuration together.
The load test#
The method, on the Xeon E-2236 test host:
setup.sh --centrifugowith the memory engine, behind nginx with the 8k tunnel buffersEvery message was delivered (delivery ratio 1.0) and no socket dropped at any step.
Centrifugo idles at 84 MB and grows about 43 KB per socket; nginx grows about 29 KB per tunnelled socket with the 8k buffers. Those two numbers became the memory plan: the script books 96 MB plus 72 KB per expected socket off the services’ share, which is 800 MB for 10,000.
Delivery is in single milliseconds to 4,000 sockets. At 10,000 the p50 rises to 36 ms, which is still well inside what a reader notices for a new post appearing, and part of that is probably the load generator competing for the same CPUs.
Why the default is 10,000, not 50,000#
The first version of
conns=autoplanned 50,000 sockets on any host above 16 GB. On the test host that booked 1.2 GB of memory and sized nginx for 40,201 connections. A first ladder to 50,000 sockets delivered 99.995 % of messages, but at a 1.3 s p50, with the load generator, nginx and Centrifugo all sharing the machine’s 12 threads.I suspect much of that number is the test rig rather than Centrifugo, but I didn’t separate the two, and it made me ask the right question: how many sockets do my boards actually need? One socket is one open member tab, and guests never connect. My largest boards peak near 10,000 members online, and 3,000-4,000 is a normal day. Planning for 50,000 reserved memory those hosts will never use.
So
conns=autonow plans 10,000 at most (2,000 below the 4 GB memory class). The value sizesLimitNOFILE, the memory booking and nginx’s connection table; it never caps members.conns=Nplans more for a board that needs it. One node still tops out around 64k sockets either way, because each one is an nginx connection to port 8000 from the ephemeral port range.What the live test found#
The unit tests passed before the first live run. The live run on a fresh XenForo 2.3.14 board found eight things they hadn’t:
config.json.new, and Centrifugo picks the format from the extension, so it refused it with “Unsupported Config Type”. It’s staged asconfig.new.jsonnow, the warning carriescheckconfig’s own message (secrets masked), and the unit-test stub refuses an unknown extension the way the binary does.wwwredirect. It took the first origin name, which belonged to the redirect block and answered 301. It now uses a name from the block that serves the forum.worker_connectionsonly grew on a later--optimize.--centrifugonow ends with the same closing--optimizepass that--domainruns: 27,701 connections on the test host for 10,000 member sockets, since nginx holds two connections per WebSocket.conns=autoplanned 50,000. Covered above.currentinps,topand the journal, because the unit ran it through a symlink of that name. The link is namedcentrifugonow, and an oldcurrentlink is moved over on the next run.The final live checks: the forum’s origin gets 101 through nginx and a foreign one 403, a POST to the WebSocket gets 403,
/connection/sse,/healthand/apinever reach Centrifugo, and two re-runs changed no byte and restarted nothing.What I didn’t measure#
engine=valkeyruns Centrifugo’s Redis engine on the script’s Valkey socket. It ships off by default until a live A/B decides, and that A/B hasn’t run./connection/websocketpass-through, which the script does not add for you.Installing it is
setup.sh --centrifugoon a host that already runs the script’s nginx. After that,--optimizekeeps it planned and wired like everything else.