I have a Rails6 application that I run on a normal server (no docker). I connect to Puma via socket.
There are static files in folders
/var/www/site/current/public/ as robots.txt Or favicon.ico
/var/www/site/current/public/packs/**/*.css (png, jpg etc) files
(eg. URL /packs/media/images/site-100500.png)
and there are dynamic URL addresses like
/user/{ID}
/page/{PAGE_NAME}
How do I set it up to take turns checking:
if the file exists on the disk in public folder then to give out
In Caddy v2, request matching is exact-match, so a path matcher of / for your proxy will only match requests to the root of your site and nothing else. Just remove that /. See the request matching docs here for more detail on how it works:
You can also move the socket to the same line instead of using the to subdirective, it reads cleaner in my opinion:
Anyways, what you want to do is something like this:
@notStatic {
not file
}
reverse_proxy @notStatic unix//var/www/site/shared/tmp/sockets/puma.sock
This will make the reverse_proxy only match requests when the requested path is not a file that exists on disk. You could further limit this to only requests in your public dir by adding not path /public/* in the named matcher.
The reason for taking the approach of excluding requests to reverse_proxy is because the default directive order for Caddyfile config has reverse_proxy sorted before file_server, so you either need to exclude the reverse_proxy from handling certain requests that you want to have served statically, or you need to reorder the file_server before reverse_proxy and add a matcher on file_server to only match requests to /public/* etc.