Serve static files matching string from domain

Would something like this work for you?

*.example.com {
  tls {
    dns [provider]
  }
  root * /usr/share/caddy
  rewrite * /{labels.2}{uri}
  file_server
}

Requests for myapp-desktop.example.com are served from a wildcard certificate (requires DNS validation for automation) and rewritten to be served from /usr/share/caddy/myapp-desktop/ on disk.

Likewise requests for long-string-mobile.example.com are served from /usr/share/caddy/long-string-mobile/.

The benefit to this setup is no changes to Caddyfile config when prototyping features; requests for nonexistent features on disk result in 404s. You simply put the files in /usr/share/caddy/ when you’re testing and remove them when you’re done, naming them appropriately and then accessing by the same name you use for the directory containing the files.

For reference, I used a very similar trick back in the day (for Caddy v1!) for the purposes of local web development: [Guide] Local web development setup on a Mac (Caddy v1)

2 Likes