Reverse proxy to a localhost isn't behaving as I expect

Unfortunately, yes, your expectation wasn’t accurate. It is true that greater specificity matters in certain circumstances, but not universally so.

As a rule of thumb, path specificity matters between duplicates of the same object. Site blocks, for example, sort by length of the site address - so you can expect foo.example.com to match before *.example.com. Multiple reverse_proxy instances are another example - you can expect reverse_proxy /api http://api to match before reverse_proxy http://upstream does as the path matcher is sorted by specificity.

Any duplicated directives that don’t have a path matcher (implicit or otherwise) would be sorted in order of appearance in the Caddyfile.

But respond will always operate before reverse_proxy if it can, as per the directive order, meaning the reverse_proxy is the ignored directive in your example.

The link Mohammed supplied outlines how it works:

For ease of use, the Caddyfile adapter sorts directives according to the following rules:

  • Differently named directives are sorted by their position in the default order.
    […]
  • Same-named directives are sorted according to their matchers.
    […]
4 Likes