Something wrong in caddyfile

Caddy does not, by default, alter the URI of the request in any way when passing it through to the upstream server.

That means when you request https://example.com/api/, Caddy requests http://xx:xx:xx:xx:port/api/.

This means the second will be shown (xx:xx:xx:xx:port/api/).

You’ll need to configure Caddy to alter the URI yourself to remove the extra part (/api), before proxying the request.

You can do that with the uri directive (e.g. uri strip_prefix /api) or via the handle_path directive, which uses the uri directive behind the scenes.

I’d also recommend using a strict rewrite/redir to treat the literal /api as if it were /api/, so that you can avoid an /api* matcher accidentally catching /apifoo (clearly not intended).

A working configuration might look something like:

https://example.com {
  tls cert.crt private.key
  redir /api /api/
  handle_path /api/* {
    reverse_proxy xx:xx:xx:xx:port
  }
}

You might run into an issue where links and Location headers returning from the upstream server are mismatched to what Caddy now expects to receive, but you might not, depending on the upstream app. If you encounter some weird behavior here, read up on the “subfolder problem”.

See:

2 Likes