I think you’re looking for something like this:
:80 {
handle_path /ui/account* {
root * ../accounts/ui/build
file_server
}
handle /api/account* {
reverse_proxy localhost:6677
}
handle {
respond "Not found" 404
}
}
When Caddy handles requests, it doesn’t automatically strip the path prefix when you use a matcher. The file_server
handler assembles the path on disk by joining the root
with the request path. So with your Caddyfile, the path Caddy was looking for on disk was ../accounts/ui/build/ui/account
. To deal with this, we can use handle_path
, which makes a subroute (i.e. handle
) but also strips the path prefix on the given matcher.
Also, your config was leaving some paths unhandled. The default behaviour of Caddy is to return an empty 200 response if no handler was configured to handle that request. To resolve this, we use a handle that responds with a 404 as a fallback for the requests.
We also wrap the proxy in handle
, because in the Caddyfile, directives are sorted by the directive order defined at the link below. You’ll notice that respond
comes before reverse_proxy
, so if we didn’t wrap them both in handle
, the respond
would always happen before reverse_proxy
, making Caddy always serve 404s when you’d expect it to proxy.
If the Caddyfile adapter encounters the same directive more than once, it will sort them by their path matchers, longer first. This makes sure that the proxy handle will be ordered first, here.
Note that I’m assuming you’re running Caddy v2.2.0, there was a fix included in that version which ensures that handle_path
and handle
are sorted as though they are the same directive. This is important here so that the handle
for the 404 doesn’t get sorted ahead of the file_server
one.
This is just one way to solve this in the Caddyfile. There are other approaches that could be taken, for example using route
to override directive order, or using not
matchers to exclude relevant paths, but I think this solution with handle
blocks is the easiest to reason about.
Next time, please fill out the help thread template, it helps us a lot to understand the context of what you’re trying to do, and I wouldn’t have had to assume a specific Caddy version that you’re using. Running Caddy in different environments like Docker also brings certain assumptions into play.