Related posts
- clang, PHP 8.4 and the missing zlib: the last round of compiled PHP
The final round of the compiled-PHP question: CentminMod's function alignment on its own, a clang build and PHP 8.4, with every module compiled from source. clang is the only build with a repeatable edge (+3 % on XenForo), 8.4 is slower than 8.5, and my earlier source builds were missing zlib.
- Who compresses a logged-in XenForo page? 12 % of PHP, one fastcgi_param, and a clock that lied
Profiling the member pages of a XenForo forum showed zlib eating 12 % of PHP-FPM CPU: XenForo gzips its own pages at level 9 inside the worker. Moving that to Angie gave +5 % on the forum index and +9 % on thread pages. Along the way, a CPU turbo budget faked a 27 % speed-up, and three other ideas did not survive measurement.
- Deleting my bot filter: 2,400 lines, one proxy hop, and what an Angie map actually costs
I removed the optional Anubis bot filter from my provisioning stack: what it cost, how to migrate a host without taking the sites down, and an honest measurement of what is left in the Angie request path. The maps were already lean; one regex if was not.
“Which compression should my web server use?” usually gets one answer for the whole site. On a PHP origin there are really three different jobs, and they want three different answers:
This cycle of my provisioning script audit measured all three with gzip, Brotli and zstd. In short:
.br(Brotli 11) +.gzThe same audit also found a way to make the guest cache store a member’s page and serve it to everyone. That’s at the end.
The test bed#
cpu.statbefore and after each run, divided by the requests completed), so it measures the server, not the load generator.First, a measurement mistake worth knowing about#
My first baseline ran each encoding’s three runs back to back. The first encoding of a page came out up to 24% faster than the others, even when all of them were served the same stored bytes. The CPU had more boost headroom for whichever run started first.
Every comparison below therefore rotates the encodings (or configurations) within each round: A, B, C, then A, B, C again. Rotation shrinks the effect but doesn’t remove it: whatever runs first in a round still gets a small head start. I put the old default first, so if anything the gains below are understated. If you benchmark anything on a desktop-class CPU, rotate too, or you’ll measure the order rather than the change.
What the clients actually ask for#
Accept-Encoding: gzip, deflate, br, zstd.gzip, deflate, br.br, gziponly. zstd is something Cloudflare offers at its own edge, after re-encoding your response (Cloudflare docs).So for a site behind Cloudflare, zstd at the origin only ever reaches visitors who connect to the origin directly. Keep that in mind for the member-page numbers.
1. Static files: stop compressing them per request#
Before, every request for a script or stylesheet was compressed on the fly with Brotli level 4. The XenForo core script (177 KB) is a good example (rotated A/B,
Accept-Encoding: br, gzip):.brfile (Brotli 11)The 99th-percentile latency fell from 76 ms to under 5 ms.
The precompressed file is served faster than the uncompressed one, because it’s a quarter of the size. It’s also 15% smaller than the per-request Brotli, since level 11 is affordable when it runs once.
WordPress’s block stylesheet went from 4,290 to 93,204 requests per second, and jQuery from 3,620 to 81,741. Clients that only accept gzip get the
.gzfile: the same speed-up, but only 4-10% fewer bytes.Why Brotli and not zstd for the files#
I expected zstd to win, so I measured it. Over 10 MB of XenForo scripts:
For the same build time, Brotli 11 is 6% smaller. zstd’s strength is speed, and speed doesn’t matter for a file you compress once. So the script writes
.brand.gzsiblings only.One Angie detail matters here. With the zstd static module loaded, a browser that accepts both is served the larger
.zstfile. Without it, the browser gets the.brfile. I don’t load it.Precompression without stale files#
Precompressed siblings have one classic failure: the source changes and the sibling doesn’t. The audit found four ways that could happen:
tar,rsync -aorcp -prestores an older modification time, so “source newer than sibling” never fires.gzip_statickeeps serving them.The cron helper now rebuilds a sibling whenever the two modification times differ. It checks the source’s size and time before and after compressing, and it removes what it made when the feature is off or a site goes away.
The first pass over a XenForo + WordPress host took several minutes, so it now runs in the background instead of holding up the script.
2. Member pages: zstd where the browser accepts it#
Removed again in 1.21.59: see the update at the top. Behind Cloudflare, Brotli 4 is what member pages get.
A logged-in XenForo thread page (127 KB of HTML) can’t be cached. It’s rendered by PHP and compressed by Angie on every request. Here are the three encoders at the levels that matter, rotated (from two rotation sessions, each with its own Brotli 4 reference: 380 and 379 requests/s):
zstd 6 is smaller and cheaper than Brotli 4, and zstd 5 matches its size at a third less CPU. I picked zstd 3. It costs 6% more bytes than Brotli 4 (about 1.1 KB on this page) for 61% less Angie CPU per request (about 77% less for the compression itself), and its time to first byte is 1 ms below Brotli 4’s. On a server where PHP is the bottleneck, CPU is the scarcer resource. Level 4 adds CPU for almost nothing.
The module order trap#
With both modules loaded, my first test sent Brotli to every client, including ones that listed zstd first. In Angie (as in nginx), the output filter that loads last runs first. My zstd module file was named
40-zstd.conf, which sorts before50-brotli.conf, so Brotli always got there first. Renamed to60-zstd.conf:gzip, deflate, br, zstd(Chrome, Firefox, Edge)br, gzip(Cloudflare)gzip, deflate, br(Safari)zstd;q=0, brIf you load both modules, check which one actually answers.
What did not help#
WordPress and Chevereto compress their own pages in PHP (
zlib.output_compression), so the guest cache can store the compressed copy. I tested letting Angie compress member pages instead, as the script already does for XenForo. For a browser: 145 vs 144 requests per second, inside the run-to-run spread. For the Cloudflarebr, gzippath Angie was about 3% slower (145 vs 141). No change shipped.3. The guest cache: already right, with one small fix#
A cached page is compressed once, when it’s stored, and then sent to every visitor. The cost of the encoder barely matters; its size does. The stored body is gzip, because that’s what PHP and XenForo produce. Any client that accepts gzip, which is every browser and Cloudflare, gets it in about 0.1 ms of CPU.
Sizes of the actual pages:
4. A member page served to guests#
Each cycle also runs a code audit. This time every finding went to two independent reviewers, each told to try to refute it. The most serious result this time was in the guest cache.
The cache decides “member or guest” by looking for the session cookie, for XenForo
xf_session=. PHP, however, rewrites cookie names before the application sees them:., space and[become_. So a member who sends their own session ID asis a guest to the web server and logged in to XenForo. On the test server, two such requests stored the member’s thread page, and the next visitor without any cookie received it from the cache: the member’s name, account links and form tokens. The same works for
wordpress.logged.in_<hash>.It needs a valid session of your own, so it doesn’t steal anyone else’s account. It does put your logged-in page in front of every guest for the cache lifetime, and it breaks their forms.
The fix accepts every separator PHP folds, so the pattern matches
xf.session,xf session,xf[sessionandxf_sessionalike. It also doesn’t depend on thexf_prefix, so forums with a custom cookie prefix are covered. A cookie name containing%is treated as a member, as a margin. I checked the common guest cookies (Google Analytics, Matomo, Hotjar, consent banners); they stay cacheable.If you run any full-page cache in front of PHP with a cookie-based bypass, test it with a dotted cookie name.
The audit also found that the script’s own optimizer stripped two other cache safeguards on every run, and that turning on Anubis could open an origin that was meant to accept only its front proxy. Both are fixed, with tests.
PHP 8.6#
The missing Redis, igbinary and Imagick extensions for PHP 8.6 are now packaged. An 8.5 → 8.6 → 8.5 round trip ran clean with all three applications, in under two minutes each way.
Null, no longer compile.What this does not prove#
What changed#
All of this shipped in release 1.21.58 of ubuntu-setup-script and its Angie configuration. If you tune compression yourself: