I’m trying to translate a nginx config file into a caddyfile. Nginx is serving static content from two different directories. This is a minimal and functional nginx config.
The requests would come as /file1, /file2, etc… And this nginx server would go and look for the file in both directories.
This uses a named matcher with the file matcher to check if the requested file exists in /static1; if so, it sets the root to /static1. Else, it’ll set root to /static2.
There’s a few different ways this could be expressed in the Caddyfile, but that’s probably the simplest, if you don’t have any more requirements than that.
Could you tell me a little bit about the other ways? I’m completely new to caddy, so maybe it might come in handy in the future. The nginx file I’m translating is huge. This was just one part of it.
That’s neat. One last question, so that I could avoid a mistake in the future.
I’m curious why try_files didn’t work. Was I using it in a wrong way? I tried both {uri} and {path} there.
Caddy’s try_files is a path rewrite. Caddy’s default root is your current working directory. For try_files to work, your root would need to be set to /, but that’s… a terrible idea, for security reasons.
There’s also the difference of the root directive, vs the root subdirective of many directives or the file matcher. The directive sets it “globally” whereas the subdirective is an override to the global one, for that directive.