First option - you can introduce a rewrite, with the proxy as the fallback option, that prioritizes serving static files. This is a broad, “serve a static file if it exists, otherwise, proxy the request”.
example.com {
root /var/www
rewrite {
to {path} {path}/ /proxy-target{uri}
}
proxy /proxy-target/ myproxy {
websocket
without /proxy-target
}
}
Second option - instead of prioritizing existing files, you can specifically rewrite any request that isn’t /index.html or /subdir to the proxy target.
Note that the canonical way to request /index.html is simply to request /, and Caddy’s server will redirect clients in this manner, so you can expect a client will always request / if they want /index.html.
example.com {
root /var/www
rewrite {
if {path} not /
if {path} not_starts_with /subdir
to /proxy-target{uri}
}
proxy /proxy-target/ myproxy {
websocket
without /proxy-target
}
}