Objlab
← News
Linguaggi e compilatori

Zeroserve: un server web che non richiede configurazione e che può essere gestito tramite script con eBPF

Sintesi redazionale: URL dell'articolo: https://su3.io/posts/introducing-zeroserve URL dei commenti: https://news.ycombinator.com/item?id=48425723 Punti: 113 # Commenti: 28. Fonte originale: https://su3.io/posts/introducing-zeroserve

<p>*Disclaimer: This article is co-authored with GPT-5.5 and Claude Opus 4.8.*</p><p>zeroserve is a small, fast, zero-config HTTPS server. You hand it a tarball of a website and it serves it - over HTTP/2 and TLS 1.3, with hot reload and a tiny resident footprint. The twist is that you can drop eBPF programs into the tarball and they run on every request, in userspace, as sandboxed middleware - rewriting, authenticating, and rate-limiting requests, or reverse-proxying them to a backend when you want it to act as a gateway in front of your app.</p><p>In short:</p><p>**Fast**: on one core it beats nginx across most workloads - small and large static files, scripted middleware, and small-response proxying, all over HTTPS.**Efficient eBPF scripting**: scripts are JIT-compiled to native code and sandboxed in userspace, cheap enough to run on every request.**Program-as-configuration**: your eBPF program is the whole configuration, deciding what happens to each request.: every network and disk operation is submitted through`io_uring`</p><p>throughout`io_uring`</p><p>.**Modern TLS in the box**: TLS 1.3, HTTP/2, Encrypted Client Hello, SNI certificate selection, and JA4 fingerprinting.**Simple to operate**: serve a whole site from one tarball and hot-reload it (and the TLS material) with a`SIGHUP`</p><p>.</p><p>It’s meant to be an alternative to nginx and Caddy, and the design bet is about *configuration*. Those servers give you a declarative config language - `location`</p><p>blocks, `rewrite`</p><p>rules, `map`</p><p>directives, `try_files`</p><p>- and then, once the declarative language hits its limits, an optional scripting runtime bolted on the side (Lua, or Caddy’s plugins). Behavior ends up split across two layers: directives that quietly grow their own control flow, plus scripts that run somewhere in the request lifecycle you have to keep in your head.</p><p>zeroserve collapses that into one thing. There is no config file. The eBPF program *is* the configuration - a single, ordinary, sandboxed program that sees every request and decides what happens: routing, headers, auth, rate limiting, proxying. I want the whole request path in one program I can read top to bottom.</p><p>## One tarball, served in place</p><p>The whole site is a single `tar`</p><p>file. zeroserve indexes it on load - building a `path -&gt; byte-range`</p><p>map - and then serves files by issuing byte-range reads against the tarball itself. Nothing is ever unpacked to disk. The site lives entirely in that one file, so there’s no document root for a stray `location`</p><p>rule to expose, and a deploy is a single atomic file swap. To package a directory:</p><p>```<br>zeroserve --pack ./public &gt; site.tar<br>zeroserve --addr 0.0.0.0:8080 site.tar<br>```</p><p>Deploying a new version is “replace the tarball and send `SIGHUP`</p><p>“. The reload swaps the site, the scripts, and the TLS material atomically, in the same process, with no dropped connections:</p><p>`killall -SIGHUP zeroserve`</p><p>All network and disk I/O goes through `io_uring`</p><p>(via the monoio runtime). Each instance is a single-threaded event loop. That sounds like a limitation, and per-process it is - but it’s the right shape when your scaling unit is “more processes”, and it’s why many of them coexist happily on one box.</p><p>## Scripting with eBPF, in userspace</p><p>This is the part I find most fun. Any `.c`</p><p>file you put under `.zeroserve/scripts/`</p><p>gets compiled to an eBPF object at pack time (with `clang`</p><p>and `llc`</p><p>) and runs on every request. The eBPF runs entirely in userspace: zeroserve loads the bytecode into a runtime (async-ebpf) inside its own ordinary, unprivileged process, so the kernel’s BPF subsystem and `CAP_BPF`</p><p>stay out of it. async-ebpf JIT-compiles the bytecode to native machine code (it vendors uBPF), so your “config” runs as native x86-64.</p><p>A *pointer cage* does the job the kernel verifier normally would, keeping the program from reading or writing memory it shouldn’t: every memory access in the JIT-compiled code is masked into the program’s own arena, so a stray access stays confined to the script’s own memory.</p><p>The script runs directly on zeroserve’s single event loop. To keep one slow script from stalling every other connection, the runtime is fully preemptible: a timer can interrupt JIT-compiled native code mid-execution and hand control back to the event loop.</p><p>The programming model is a chain of scripts, run in sorted filename order, sharing a per-request metadata map. If a script calls `zs_respond`</p><p>or `zs_reverse_proxy`</p><p>, the chain short-circuits. Here’s a script that runs first and enriches every request:</p><p>`#include `<br>ZS_ENTRY<br>zs_u64 entry(void) {<br>char peer[64];<br>if (zs_req_peer(peer, sizeof(peer)) &lt;= 0) zs_strcpy(peer, &quot;unknown&quot;);<br>// publish values for the HTML template pass<br>zs_meta_set(ZS_STR(&quot;visitor&quot;), ZS_STR(peer));<br>// attach a header to *every* response: static files, zs_respond, proxied<br>zs_meta_set(ZS_STR(&quot;zs.response.header.x-served-by&quot;), ZS_STR(&quot;zeroserve-ebpf&quot;));<br>return 0;<br>}</p><p>The metadata it sets does two things. Keys under `zs.response.header.*`</p><p>become response headers on everything. And other keys feed a tiny template pass: a</p><p>placeholder in an HTML file gets substituted on the way out. So you get dynamic-ish static pages without a template engine.</p><p>The helper surface a script can call is broad:</p><p>**Request inspection and mutation**: read the method, path, query params, headers, and peer address; rewrite the URI or set and remove headers before the response goes out.**Crypto and encoding**: SHA-256, HMAC-SHA256, base64, hex, and`getrandom`</p><p>.**JSON**: parse a request body, build and mutate a document tree, and reply with`zs_json_respond`</p><p>.**Rate limiting**: per-key token buckets keyed on anything from a peer IP to an API key, with state that survives hot reloads.**AWS SigV4**: signed`Authorization`</p><p>headers and presigned URLs for talking to S3 and other AWS services.**OIDC login**: a complete relying-party flow (Authorization Code + PKCE) that carries the entire login session in sealed XChaCha20-Poly1305 cookies, so you can gate a static site behind “log in with Google” while the server stays stateless.</p><p>A dynamic endpoint is just a script that responds:</p><p>```<br>ZS_ENTRY<br>zs_u64 entry(void) {<br>char path[64];<br>zs_req_path(path, sizeof(path));<br>if (zs_strcmp(path, &quot;/health&quot;) != 0) return 0;<br>zs_meta_set(ZS_STR(&quot;zs.response.header.content-type&quot;), ZS_STR(&quot;application/json&quot;));<br>zs_respond(200, ZS_STR(&quot;{\&quot;status\&quot;:\&quot;ok\&quot;}\n&quot;));<br>return 0;<br>}<br>```</p><p>Each script runs under a memory-footprint cap (256 KB by default), the runtime time-slices long-running scripts off the executor and throttles the runaways, and scripts can even call each other (`zs_call`</p><p>) up to a bounded depth. A script that spins forever stalls only its own request - the preemption timer interrupts it and the server keeps serving everyone else.</p><p>The TLS story underneath is more complete than the zero-config framing suggests: TLS 1.3 only, terminated by BoringSSL, with native **Encrypted Client Hello** (so the real SNI never appears in cleartext), SNI certificate selection from a directory, JA4 client fingerprinting exposed to scripts, and a transparent ECH relay mode that byte-for-byte forwards undecryptable handshakes to a real upstream so a protected name blends in behind a public one. That’s a lot of transport security to ship in a single zero-config binary.</p><p>## How fast is it?</p><p>I benchmarked zeroserve against nginx 1.26 and Caddy 2.11 over HTTPS on an 8-core Ryzen 7 3700X, each serving the same content with the same self-signed certificate. Because a zeroserve instance is single-threaded by design, the only fair comparison is *per core*: I pinned every server to one CPU with `taskset`</p><p>(and held nginx to `worker_processes 1`</p><p>and Caddy to `GOMAXPROCS=1`</p><p>; zeroserve is single-threaded already) and drove load with `wrk -t4 -c100`</p><p>from other cores, taking the median of three 10-second runs. `wrk`</p><p>speaks HTTP/1.1, so these are HTTP/1.1-over-TLS-1.3 numbers with the handshake amortized across long-lived keep-alive connections: the steady-state cost of serving an already-open HTTPS connection.</p><p>**Small static file (174 B)** - the bread and butter of static sites:</p><p>| server | req/s | p99 |<br>|---|---|---|<br>| zeroserve | 36,681 | 5.4 ms |<br>| nginx | 31,226 | 7.8 ms |<br>| Caddy | 12,830 | 22 ms |</p><p>zeroserve serves small files about 17% faster than nginx on a single core, with a tighter tail. HTML pages, small JSON, CSS - this is the case zeroserve is tuned for.</p><p>**Large static file (100 KB):**</p><p>| server | req/s | throughput | p99 |<br>|---|---|---|---|<br>| zeroserve | 8,000 | 782 MB/s | 22 ms |<br>| nginx | 7,600 | 773 MB/s | 28 ms |<br>| Caddy | 6,084 | 590 MB/s | 44 ms |</p><p>All three are close here, with zeroserve a hair ahead at around 780 MB/s on one core. nginx’s usual trump card for large files is `sendfile()`</p><p>, which splices file pages from the page cache to the socket with zero userspace copies. Under TLS that path goes unused: the bytes have to be encrypted in userspace anyway (short of kernel TLS, which all three leave off), so every server is bound by the same encrypt-and-write loop, and zeroserve’s `io_uring`</p><p>read-and-write path is a touch faster at it.</p><p>## eBPF vs Lua</p><p>The obvious comparison for the scripting is nginx + LuaJIT (`ngx_http_lua_module`</p><p>), the usual way to run fast code inside a web server. So I wrote the equivalent Lua for two cases and put them head to head.</p><p>One tuning knob matters a lot here. zeroserve ships with a conservative default: it arms the script-preemption timer every **2 ms**. Fine granularity makes it quick to throttle a misbehaving script, but it taxes every well-behaved one - at the default, eBPF trails nginx Lua on a fully dynamic response (about 32k req/s against 41k). Bumping `--preempt-timer-interval-ms`</p><p>to 10 recovers ~40% of scripting throughput and turns that around:</p><p>**Per-request header-injection middleware** (script runs, static file is still served):</p><p>| engine | req/s | p99 |<br>|---|---|---|<br>| zeroserve eBPF (10 ms) | 43,709 | 5.1 ms |<br>| zeroserve eBPF (2 ms default) | 31,334 | 6.7 ms |<br>nginx Lua (`header_filter` ) | 28,653 | 8.4 ms |</p><p>**Fully dynamic JSON response:**</p><p>| engine | req/s | p99 |<br>|---|---|---|<br>| zeroserve eBPF (10 ms) | 46,945 | 4.5 ms |<br>nginx Lua (`content_by_lua` ) | 41,231 | 6.4 ms |<br>| zeroserve eBPF (2 ms default) | 32,393 | 6.7 ms |</p><p>At the 10 ms interval, tuned eBPF wins both cases. On the middleware case - a script shaping an otherwise-static response - it beats nginx Lua by about 50%, with a tighter tail. On the fully synthetic response it edges nginx’s heavily-tuned `content_by_lua`</p><p>too (47k against 41k). Both engines compile to native code (LuaJIT is a tracing JIT; async-ebpf JITs the eBPF through uBPF), and with TLS encryption as a shared per-request cost, the tuned eBPF path comes out ahead on throughput. At the 2 ms default, eBPF keeps the middleware win but gives up the synthetic-response lead, so I’d run production scripts at 10 ms.</p><p>## As a reverse proxy</p><p>Serving files is half the job; the other half is proxying to a backend, which is the main reason most people reach for nginx or Caddy in the first place. zeroserve does it from a script - `zs_reverse_proxy(&quot;http://127.0.0.1:9000&quot;)`</p><p>- and keeps a pool of upstream connections (up to 128 per backend, 30 s idle) and reuses them across requests.</p><p>Getting a fair fight here takes care: nginx’s famous default closes upstream connections after each request, so keep-alive is enabled explicitly (`keepalive 128`</p><p>, `proxy_http_version 1.1`</p><p>, and a cleared `Connection`</p><p>header), with Caddy reusing connections as it does by default. Each proxy terminates TLS on a single core and forwards to a shared plaintext backend, a separate 2-core server that sustains 100k req/s on its own, so the measurement isolates the proxy’s own overhead.</p><p>Proxying a small (174 B) response:</p><p>| proxy | req/s | p50 | p99 |<br>|---|---|---|---|<br>| zeroserve | 26,486 | 3.3 ms | 8 ms |<br>| nginx | 21,761 | 4.2 ms | 10.5 ms |<br>| Caddy | 7,683 | 10.3 ms | 33 ms |</p><p>zeroserve’s pooled `io_uring`</p><p>_(testo troncato)_</p>