I’m trying to replicate my old Nginx configuration that used the try_files
directive to attempt to serve files from my public folder before passing the request to my Rails app.
Here’s my current Caddyfile:
www.example.com {
root /srv/webapp/current/public
tls webmaster@example.com
gzip
cors /assets https://www.example.com
proxy / unix:/srv/webapp/shared/tmp/sockets/puma.sock {
except /assets
transparent
}
}
As with most Rails applications, my public folder looks like:
public/
|-- 404.html
|-- 422.html
|-- 500.html
|-- apple-touch-icon-precomposed.png
|-- apple-touch-icon.png
|-- assets -> /srv/webapp/shared/public/assets
|-- favicon.ico
|-- robots.txt
|-- sitemap.xml.gz
|-- system -> /srv/webapp/shared/public/system
\`-- uploads -> /srv/webapp/shared/public/uploads
My current Caddy configuration serves files out of the assets folder with no problem, because of except /assets
in the proxy block. However, I also want Caddy to serve all other files in the public folder, 404.html, 500.html, etc.
I see no good way of accomplishing this aside from listing all the files as arguments to the except
directive, which just seems hackish.
How can I configure Caddy to serve a file if it exists, and pass the request to the proxy if the file does not exist?