FROM caddy:2.2.1-builder AS builder
RUN xcaddy build v2.2.1 \
--with github.com/caddy-dns/gandi
FROM caddy:2.2.1
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
d. My complete Caddyfile or JSON config:
api.mydomain.de {
root /v1/chargers /srv/data
file_server {
index index.json
}
log {
format console
}
}
3. The problem I’m having:
I want the JSON file in /srv/data/index.json to be served when the url https://api.mydomain.de/v1/chargers is called. However Caddy is returning a HTTP 404.
If I change the root directive in the config to root * /srv/data calling https://api.mydomain.de/ will result in the delivery of the file.
Please note that path matching is exact-match in Caddy v2, so /v1/chargers will only match /v1/chargers, but not /v1/chargers/foo, in case that matters.
Also note that any other unhandled paths will result in empty HTTP 200 responses unless you do something else with them.
In V1 I was doing it like this, which seems far easier than the new method, is it really the only way to do what I’m looking for?
api.mydomain.de/v1/chargers {
root /srv/data
index index.json
}
I would also suggest to output the path in the logs where caddy is looking for the file on the file system. This would tremendously help debugging these kind of problems by myself.
Right - the file server handler is triggering a redirect to canonicalize the URL in this case. Basically if it sees that the request is for a directory, it will redirect to append a / to the path. Since the path was stripped by handle_path, the file server sees the path as just an empty string.
So the solution is to put a rewrite inside the handle_path to make sure the file server looks at your JSON file instead of the directory.
this actually solved my problem, I still think it’s a rather cumbersome syntax and I wouldn’t have come to that solution without your help just by reading the documentation which could easily be done in V1 of caddy.
Perhaps adding the path/file which is accessed on the file system level would be a good addition to the debug or error logging option.
I don’t see how it could be any simpler, frankly. Every part of it has a purpose and they do it in a consistent way.
I strongly disagree. Caddy v1 had huge limitations that made many things impossible to do. Every directive in v1 had their own concept of request matching which made it very inconsistent to write, you’d need to use rewrites to chain matching logic, etc. It suffered from having every new feature being tacked on one after the other with a lack of cohesion.
Caddy v2 was a complete rewrite with a huge focus on the underlying design to build a system that would make it possible to do much more powerful things in a more pluggable way. I think it’s been a huge success.
Yeah I think you’re right - we generally added debug logs on a per-need basis, probably a good idea to add one for the filename the file server is attempting to read.