Related posts
- 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.
- Dropping the packages: compiled nginx, PHP and Valkey, and what the migration tests broke
My setup script now builds nginx, PHP and Valkey from source and installs none of them from packages. Live servers migrate once, on --optimize. Testing that migration and a PHP downgrade chain found eight bugs, including a build log that grew to 1.7 TB. A day later, 1.22.1 dropped pre-compressed static files.
- Why my from-source PHP was slower, and the one-line answer: ICU
A from-source php-fpm executed 9 % fewer instructions per page than a clang build using Sury's extensions, yet ran 1-2 % slower. The cause was ICU: the intl extension made the binary 102 MB and multiplied data-TLB misses by 4.5. Without it, the merged build is the fastest: +11 % on logged-in XenForo pages.
My setup script compiles nginx from source for every site it hosts: XenForo forums, WordPress blogs, Chevereto galleries and a few plain vhosts. Release 1.22.45 changes something every visitor eventually sees: the error page.
Until now, a missing file or a broken backend got nginx’s built-in page. That’s a bare
<h1>404 Not Found</h1>in Times New Roman, with no way back to the site. A few vhosts had their ownerror_pagelines pointing at a page in the docroot, and some of those pages no longer existed.What the page looks like#
It shows the status code, one line for that status, and two actions: Home goes to
/, and Try again reloads the same URL. There’s no site name, logo or domain, so it suits every site on the server. It uses the system font and follows the visitor’s light or dark setting. It fits a phone screen and carriesnoindex, so search engines don’t keep the URL. There’s no JavaScript, no web font, no image and no second request: a bot hammering random URLs gets one small response and nothing else. The page is about 1.3 KB.Why compile it in instead of configuring it#
The obvious way is an
error_pageinclude in every vhost plus an internal location that serves a static file. I started there and dropped it. That approach needs a line in every vhost, including the ones operators wrote by hand. It costs a file lookup and an internal redirect on every error. And it has awkward edge cases: anauth_basicor a vhost-widedenyalso blocks the error page itself, and XenForo’s guest cache uses anerror_page 418hop that stops a second error page from firing.The script already patches nginx’s source before it builds. That’s how it removes the
Serverheader and the “nginx” line under every error page. nginx keeps its built-in pages as plain strings in one source file, so the same patch now swaps in the new page for 401, 403, 404, 500, 502, 503 and 504. The result:error_pagestill wins.The patch checks the source before changing anything, like the branding patch. If a future nginx release moves one of those strings, the script refuses that version and builds the last known-good one instead. After every build, it starts the new binary on a spare port and checks that a 404 and a 502 come back as exactly this page, byte for byte.
The first version was 20 % slower#
Every change to this script has to pass an A/B test with no loss beyond noise. The test server flooded both binaries with random URLs: 5 rounds, clock pinned at 3.6 GHz, arm order rotated each round.
The first version lost badly when the client asked for compression: 17-21 % fewer requests per second. nginx’s own error page is about 150 bytes. Compressing 1.9 KB on every request costs far more, and both gzip and Brotli did that work for every 404 a bot triggered.
The fix was to compress the page once, at build time. Each page is now stored twice in the binary: plain, and gzipped (about 730 bytes). When a client accepts gzip, nginx sends the stored bytes with
Content-Encoding: gzip. It uses nginx’s own rules for that decision, the ones behindgzip_http_version,gzip_proxiedandgzip_disable. A response that already has an encoding is skipped by both compressors, so no CPU goes into compressing error pages any more.That left a smaller cost for clients that don’t ask for compression: +2.3 % nginx CPU per request, just from sending more bytes. Trimming the page from 1.9 KB to 1.3 KB brought that within noise. The final numbers:
My servers sit behind Cloudflare, which always asks the origin for gzip, so real traffic takes the first row. A bot flood of 404s now costs the server less than it did with nginx’s tiny default page.
Getting past Cloudflare’s own error page#
Cloudflare treats origin errors in two ways. It passes an origin’s 500 and 503 through to the visitor. It replaces an origin’s 502 and 504 with its own branded “Bad gateway” page, unless you’re on Enterprise with Origin Error Pages turned on. That would hide the new page exactly when it matters most: PHP-FPM down, or a backend timing out.
So the compiled-in 502 and 504 pages go out with status 503. The page title and text still say “Bad gateway” or “Gateway timeout”, so the visitor (and whoever reads the page) still sees what went wrong. The access log records 503; nginx’s error log and
$upstream_statusstill hold the real cause. This only applies to nginx’s own pages. If an application sends its own 502 with its own body, it passes through untouched.Errors Cloudflare generates itself (520-527: origin unreachable, connection timed out) never reach the server. Those need Cloudflare’s Custom Errors.
Old error_page lines get out of the way#
A vhost line like
error_page 404 /404.html;would hide the new page, and a pointer to a missing file is worse. On the test server, a legacy lineerror_page 500 502 503 504 =503 /5xx.html;pointed at a file that no longer existed. So when the backend went down, visitors got a 404.Once the running nginx has the page,
--nginxand--optimizecomment out such lines. A line qualifies only when it’s purely cosmetic: every code is one of the seven, and the target is a static.html,.pngor similar file. It becomes# setup-error-page-off: error_page ...and the report lists the file and line. Anything that routes stays: a named location likeerror_page 404 = @backend, a PHP fallback like/index.php, a redirect,=200, or other codes such as XenForo’s 418 guest-cache hop. To keep your own page, uncomment the line and add# keep. The edited configuration must passnginx -t, or every file is put back. After--optimize, that same test vhost answered a backend outage with 503 and the Bad gateway page.Nothing else changed#
The patches only touch the path nginx uses for its own error pages. To make sure, I sent the same 20 requests to the old and new binaries and compared the status, headers and body of each:
error_pagefallback.All 20 matched. The unit suites passed (3,926 and 118 checks), and the test server went back to its recorded clean state afterwards.
Updating is the usual
--optimize. The build recipe changed, so every server rebuilds nginx once, and the same run tidies the vhost lines.