Caddy 2 reverse_proxy to path

In Caddy 1, we were able to use

proxy / https://host.com/path

To reverse to a host+path.

I encountered the same error as this issue:

https://github.com/caddyserver/caddy/issues/2905

But I couldn’t understand the conclusion of the issue.


My use case is the following. I have a an Object Storage/S3 bucket at OVH. It is available at this URL:

https://storage.de.cloud.ovh.net/v1/<id>/mstdn-media

An object is available at:

https://storage.de.cloud.ovh.net/v1/<id>/mstdn-media/file.jpg

With this config for media.mstdn.io

proxy / https://storage.de.cloud.ovh.net/v1/<id>

The users of my app were able to access it as https://media.mstdn.io/mstdn-media/file.jpg.

With Caddy 2, I can’t specify a pass for reverse_proxy.

With this:

reverse_proxy https://storage.de.cloud.ovh.net

My Caddy will proxy file.jpg to

https://storage.de.cloud.ovh.net/mstdn-media/file.jpg

and not

https://storage.de.cloud.ovh.net/v1/<id>/mstdn-media/file.jpg

The /v1/<id> is missing and <id> has to stay private so I can’t update my app to add it in the user-facing URL: Caddy has to add it.

Is there a directive for Caddy 2 that allows me to do that? rewrite maybe? I searched the documentation but couldn’t find anything.

Thanks!

Yep, to add a prefix, all you need is to do a rewrite like this:

rewrite * /v1/<id>{uri}

You might need to use a matcher if you have other things being handled by the same site block. You didn’t post a full Caddyfile so I won’t try to make any assumptions here other than you’re only proxying in that site block.

1 Like

Perfect! This is exactly what I was looking for. Seems much more elegant than the Caddy 1 way.

Here is my Caddyfile for reference:

media.mstdn.io {
  import ../snippets/tls.conf
  encode zstd gzip
  log {
    output file /var/log/caddy/media.mstdn.io.log
    }
  }
  rewrite * /v1/<id>{uri}
  reverse_proxy https://storage.de.cloud.ovh.net
}
1 Like

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