How to reverse_proxy if not file?

1. Caddy version (caddy version):

Caddy 2.1

3. The problem I’m having:

How to repeat simple configuration from nginx with ‘try’ method.
For example, in nginx you can do this:

upstream upstream_app {}
server {
  try_files $uri @app;
  location @app{
    proxy_pass: http//upstream_app
  }
} 

First, it is checked if the file exists, and if not, it goes to the server. In my case it has to go to the proxy.
As far as I can see, the ‘try_files’ directive doesn’t do this, it can only redirect, but how to redirect to a proxy?

As if something like this:

try_files {path} reverse_proxy node:8080

You’re looking for the file matcher.

The try_files directive is a shortcut for a file matcher plus a rewrite.

Before continuing, I recommend updating to Caddy v2.2, there’s been some fixes that might help with reliability here.

So what you likely want is something like this:

@notStatic file
reverse_proxy @notStatic node:8080

If you use the file matcher without any params, it assumes you meant {path} because that’s the most common usecase.

Make sure you have root set correctly for this to work, because the file matcher concatenates the current path to the root to know where to look on disk for files.

Thank you very much! It’s more clear for me now.

There is a problem with your solution – order. I found it — V2: Unrecognized directive not, and use:

@notStatic {
    not {
        file {
            try_files {path}
        }
    }
}
reverse_proxy @notStatic node:8080
file_server 

And this works :ok_hand:

1 Like

Right, my bad, forgot the not. Caught again by boolean logic :sweat_smile:

You can shorten that to:

@notStatic not file
reverse_proxy @notStatic node:8080

This topic was automatically closed after 30 days. New replies are no longer allowed.