ubuntips

Whitespace is cheap: the nginx trim filter lost, and five setup options became automatic

Draft posted on · 6 min read

On this page
Draft commented on Dec 1, 2026

My setup script compiles nginx, PHP and Valkey from source and tunes them to the hardware it runs on. This cycle had a short list from me: try the trim filter from Tengine, make a few remaining options automatic, and stop --optimize from looking frozen while it compiles. The audit that went with it found a real security bug too.

Does stripping whitespace from HTML pay?#

Tengine, Alibaba’s nginx fork, ships ngx_http_trim_filter_module: it collapses runs of spaces and newlines in HTML and drops comments, leaving <pre>, <textarea> and (by default) inline scripts and styles alone. It sounds like free bandwidth. The module still compiles against current nginx mainline (1.31.10) as a static module, so I built it from Tengine 3.1.0, pinned by commit and checksum, into two otherwise identical nginx binaries: same recipe, same compiler flags, neither post-link optimised, so the pair differs only by the module.

Two details decide everything:

  • The filter skips anything already compressed. On my XenForo setup, nginx compresses member pages itself (XenForo’s own level-9 gzip costs too much PHP time, see the earlier measurement), so those pages get trimmed. Guest pages come out of the FastCGI cache as XenForo’s own gzip body, so the filter never sees them.
  • It runs on every response, cache hits included. nginx stores the upstream body, not the filtered one.

What it saves: a member’s forum index went from 17,695 to 16,471 bytes with gzip (-6.9 %) and from 21,516 to 19,380 with Brotli (-9.9 %); a long thread page 7-8 %. Uncompressed, 12-15 %. Pages rendered the same; after normalising whitespace, the only differences were per-request tokens.

What it costs, on a Xeon E-2236 test box (6 cores, 12 threads) with a XenForo 2.3.14 forum seeded with 20,000 members, 10,000 threads and 100,000 posts. Three rounds, arms rotated, CPU clock pinned at 3.6 GHz:

Pagewithoutwith trim
guest home, cached142,368 req/s-2.4 %
guest thread, cached107,626+0.8 %
guest home, rendered by PHP367-1.6 %
guest thread, rendered by PHP370-3.0 %
member home328-1.8 %
member thread283-1.8 %

On every PHP-rendered page the slowest trim round was below the fastest run without it. My rule for a new default is at least 2 % faster somewhere and slower nowhere, so this is a clear no. The bytes it saves are also bytes a CDN in front of the server compresses again at the edge.

To trim the cached guest pages too, XenForo’s gzip has to go, and then nginx compresses every cache hit instead of sending a stored, already-compressed body. That arm is the real lesson of the run: cached throughput fell from about 142,000 to 16,000 requests per second without the filter, and to 9,000 with it. Storing the pre-compressed page is what makes a cache hit cheap; anything that has to touch the body on every hit gives that away.

Four more ideas, four defaults confirmed#

With every binary optimised with BOLT from live traffic (worth about 8 % on the PHP pages against the same build without it), I put the current defaults against their obvious alternatives:

SettingDefaultAlternativeResult
PHP-FPM workers12 (one per thread)18, 241-5 % slower
OPcache JITtracing, 64 MBfunction mode; 128 MB11-14 % and 3 % slower
nginx workers8 (two thirds of the threads)125 % slower on cached pages
MariaDBthread poolone thread per connection2.5-4.5 % slower

Nothing changes. Recording a loss is still worth a run: the next time someone suggests “just give PHP more workers”, the number is here.

Five options that should not have been options#

--basic took server=dedi|vps, swap= and mitigations=; --php took children= and max_requests=. Every one of them already had an automatic value, and --optimize already ignored them and recomputed from the hardware. So an install with one of them produced a server that the next --optimize silently changed. They are gone: the server type is detected on every run, swap is half the RAM between 1 and 8 GB, CPU mitigations stay off (my standing choice for single-tenant, key-only servers), and the PHP pool is one static worker per logical CPU on a local database with flash storage. A command that still passes one of them stops with the release that removed it, before anything changes. Taking the explicit worker count out also removed a whole branch of the memory planner.

“Is it stuck?”#

--optimize rebuilds nginx, PHP or Valkey when a new release, a new CPU or a new set of PHP extensions makes the binary stale, and a PHP build takes several minutes. Until now the terminal showed “Optimizing installed services” and then nothing, because every build step went only into the final report. Now each step prints as it starts, with what is serving and why it is replaced:

PHP 8.5: rebuilding (now: build 8.5.13 (clang 23, recipe 5), bolt=0, ...) because
  extensions ... -> ...,igbinary,...,redis,..., redis none -> 6.3.0, igbinary none -> 3.2.16
PHP 8.5: build: still running (30 s; log: /usr/local/lib/setup-php/logs/php-8.5-....log)

A line every 30 seconds names the step, the elapsed time and the log to tail. The same goes for the BOLT passes, which sample a minute of live traffic each. The example above is real: the first --optimize after XenForo was uploaded found that the SV RedisCache add-on needs the redis and igbinary extensions and rebuilt PHP with them.

A nightly chown that root could be tricked into#

Every night the script’s cron job made everything under /var/www owned by the web user, with find /var/www ... -exec chown -h www-data:www-data {} +. -h stops chown from following a symlink in the last part of the path, but find hands chown full paths, and chown resolves every directory in them again. A compromised PHP application runs as that web user and owns those directories. It could swap a directory for a symlink to /etc between find’s read and the chown, and root would hand it /etc/shadow. The job now uses -execdir, which runs chown from inside each file’s own directory on ./name, and only touches files that are not already owned by the web user. It needs a compromised application first, but it turns that into root.

The same review tightened the XenForo guest cache. Only the front controller (index.php) feeds it now; before, a guest’s GET to any other PHP script, an add-on’s callback or job.php, could be stored for five minutes and its side effect skipped on every hit. An AJAX request (XenForo’s JavaScript sets X-Requested-With) is never served from it, and a response that starts a XenForo session is never stored. Measured against the old configuration on the same server, every page type stayed within noise (+0.7 % and -0.9 % on the cached pages over five rounds).

Smaller things#

  • Build downloads refuse a redirect to plain HTTP.
  • MySQLTuner moved out of the shell code. It was a 190 KB base64 blob that bash parsed on every run, and the payload file went from 199 KB to 9 KB.
  • A Valkey setting that Valkey 8 ignores is no longer written. The change applies online, so the cache is not restarted to drop a dead line.
  • The README now says what --optimize really does when one service’s tuning fails: it marks that service FAILED, carries on with the others and exits non-zero. It no longer claims the run aborts.

The test box went back to its recorded clean state afterwards: same 701 packages, apart from ten newer versions from Ubuntu’s own security updates.

Back to top ↑