Serve files in root before passing to proxy

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: {
    root /srv/webapp/current/public

    cors /assets
        proxy / unix:/srv/webapp/shared/tmp/sockets/puma.sock {
           except /assets

As with most Rails applications, my public folder looks like:

|-- 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?

Have you look into this ? It specifically mentioned nginx try_files.

Perhaps you could rewrite to {path} {path}/ /ruby-app{uri} and proxy /ruby-app to your app, using without /ruby-app?

This seem to work:-

root ./public
rewrite {
    if {path} is /
    to /proxy/{uri}
rewrite {
    to {path} /proxy/{uri}

proxy /proxy localhost:8080 {
    without /proxy

Based on this issue -

Although this seem to break my websocket directive.

What happens when you include websocket in the proxy directive? You might still want to include transparent as well, unless your app doesn’t use X-Forwarded headers.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.