Using Caddy to serve .well-known URLs from filesystem

I’m currently in the process of migrating away from Certbot and Apache2 towards Caddy in order to simplify my hosting setup by using less moving parts.

This works pretty well and today I was applying some finishing touches by moving .well-known redirects to the Caddy configuration for my domains.

One of those .well-known URLs however is the one used by mail clients to query configuration parameters for my mail server. This is not a simple redirect to another URL handled by an application, but rather serves an XML file from the filesystem.

I did not get it to work right away, but this is the working solution I’ve found:

handle_path /.well-known/autoconfig/mail/* {
        root * /var/www/html/autoconfig-mail
        file_server                            
}

By using handle_path, Caddy strips the given prefix from the incoming request path and hands the rest to file_server and is able to find and serve the file.

Before that, I tried using handle but then Caddy just tries to find the complete path in the configured site root, which is not working for obvious reasons.

With this, I’m almost done completely dismantling all Apache2 configurations and should be able to move more easily to something like NGINX or even using containers to reverse-proxy to. 🥳