ubuntips

One error page, compiled into nginx

Draft posted on · 6 min read

On this page
Draft commented on Dec 2, 2026

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 own error_page lines pointing at a page in the docroot, and some of those pages no longer existed.

What the page looks like#

The 404 page in light mode: a large grey 404, “Page not found”, one line of explanation, and two rounded buttons, Home and Try again

The same page in dark mode for a 502: “Bad gateway” with the same two buttons

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 carries noindex, 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_page include 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: an auth_basic or a vhost-wide deny also blocks the error page itself, and XenForo’s guest cache uses an error_page 418 hop that stops a second error page from firing.

The script already patches nginx’s source before it builds. That’s how it removes the Server header 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:

  • Every vhost, catch-all and location gets it with no configuration.
  • It still works when everything else is broken. If PHP-FPM is down, a backend has crashed or a config file went missing, the page lives inside the nginx binary.
  • The real status code stays, with one deliberate exception (below).
  • Application pages are untouched. A XenForo or WordPress 404 is rendered by PHP and passes through as before. A vhost that sets its own error_page still 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 behind gzip_http_version, gzip_proxied and gzip_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:

Random-URL floodreq/sp99nginx CPU per request
404, client accepts gzip/Brotli143,179 -> 186,998 (+30.6 %)0.74 -> 0.38 ms-42.6 %
403, client accepts gzip/Brotli150,224 -> 185,938 (+23.8 %)0.64 -> 0.39 ms
404, no compression asked194,155 -> 190,071 (-2.1 %, within noise)0.38 -> 0.38 ms+0.6 % (within noise)

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_status still 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 line error_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, --nginx and --optimize comment 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, .png or 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 like error_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 pass nginx -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:

  • 200 pages, plain, gzip and Brotli, over HTTP/1.0 and 1.1, with keep-alive;
  • HEAD, 304 and 206 range requests;
  • a large file, an image, a 301 redirect, a 405 and a 418;
  • an upstream sending its own 502 and 404 bodies;
  • a routed error_page fallback.

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.

Back to top ↑