Skip to content

Lesson 6 of 9 · 23m

Static Files, SPAs, PHP, and the Route Shapes Behind Them

Serving files correctly means compression, cache policy, fallback rewrites, and knowing when the request should never hit disk.

Caddy is perfectly capable of serving static files directly, but production file serving is mostly policy: what may be cached, what may be compressed, which paths are applications rather than files, and what happens when a path does not exist. The common mistake is treating file_server as the whole answer instead of the last step in a route.

The boring correct static site

www.sudosecurity.org {
	root * /srv/www/public
	encode zstd gzip

	@immutable path /assets/*
	header @immutable Cache-Control "public, max-age=31536000, immutable"

	header Cache-Control "public, max-age=300"
	file_server
}

root establishes the filesystem boundary. encode negotiates compressed representations. Cache headers belong close to the serving policy because the edge is where they are consistently applied. Fingerprinted assets can be immutable; HTML should not be cached for a year unless you enjoy emergency cache-busting.

Single-page applications need a fallback, not a directory listing

app.sudosecurity.org {
	root * /srv/app/dist
	encode zstd gzip

	try_files {path} /index.html
	file_server
}

try_files rewrites to the first candidate that exists. Here, real files are served directly; unknown routes fall back to /index.html so the client-side router can interpret /settings/billing. The fallback must come before file_server. If the order or matcher is wrong, every deep link becomes a 404.

Do not use this shape indiscriminately for APIs. An API typo should be a JSON 404, not a copy of your SPA shell.

PHP is a proxying problem too

blog.sudosecurity.org {
	root * /srv/blog/public
	encode zstd gzip

	php_fastcgi unix//run/php/php8.3-fpm.sock
	file_server
}

php_fastcgi is a convenience bundle: it routes PHP requests to FastCGI, rewrites appropriate paths, and serves static files. It is convenient because the missing pieces are exactly the ones people forget. Still read what it expands to with caddy adapt when debugging; convenience does not remove the need to understand the pipeline.

Never let filesystem convenience weaken boundaries

A root directory is trust boundary. Don't point root at a parent directory because "only public files are requested." One bad rewrite or encoded path regression becomes exposure of secrets, source, or backups. Deploy artifacts into a clean directory owned by deployment, and serve that directory — not your repository checkout.

Field notes

  • Caddy does not need a separate gzip preprocessing layer for normal static serving; encode is enough unless you have specialized precompressed assets.
  • If a SPA works at / but fails when refreshed at /account, you are missing the fallback or it is ordered incorrectly.
  • Cache policy is content policy. HTML, immutable assets, downloads, and API responses should not share one thoughtless header.
  • For user uploads, consider serving through the application or a constrained location rather than giving the web server a broad writable tree.

Was this lesson helpful?

Sign in to rate