I just spent a few hours reading the documentation and writing a Caddyfile for an application made using django and react (SPA using react-router). It took a while to figure out how to make the react-router work. Because of the order in which the directives are evaluated.
It works right now. But I do have one specific question. {path} or {uri} - which one should I use with try_files?
I’m also curious if there’s a better way to do this. I would appreciate your suggestions
You should probably remove all these lines. They are not necessary, and in some cases override Caddy’s defaults which are more correct:
This matcher doesn’t make sense, you can’t use a named matcher as a path. You can just remove the @front-end matcher altogether actually, and use a handle block with no matcher, because handle blocks are mutually exclusive from eachother. You can write it like this:
@backend path /api/* /django-admin/*
handle @backend {
...
}
handle {
# Fallback for any other paths, i.e. your frontend
}
{path} is probably correct here. The try_files directive uses the rewrite handler under the hood (see the expanded form) which can replace both the path and query of the URL, depending on the inputs. The query part is preserved across path rewrites if there’s no ? in the rewrite.
One last note, I’m not sure the health check stuff will be so useful for you unless you specify more than one backend to proxy to. It’s mainly useful for load balancing across multiple backends. If you just have the one, it won’t have much benefit.
Basically because not path <literally anything weird> will always be true if you never hit that path. Technically @foo can be a path, but nobody’s gonna make a request like http://localhost/@foo so