V2: Reverse proxy with sub folders

I wonder if there’s a way to use folders for Reverse Proxy without having the folder within the backend request. Example:

Request: https://exampledomain.com/folder1/test
Backend: http://localhost:8081/test

Request: https://exampledomain.com/folder2/test
Backend: http://localhost:8082/test

I’ve tried this:

exampledomain.com {
  reverse_proxy /folder1/* http://localhost:8081
  reverse_proxy /folder2/* http://localhost:8082
}

But it seems like (gotta say it seems as I don’t see the requests on the backend) that the requests go to /folder1 or /folder2 in the backend too. So for example http://localhost:8081/folder1/test instead of http://localhost:8081/test

Any help on this?

Disclaimer: I’m a total Caddy n00b so please pardon if that’s a dumb question.

Thanks a lot for a great product though :wink:

Welcome @helmi

Request matchers (i.e. your /folder1/* and /folder2/*) don’t modify the request at all; just like their name implies, they only match.

If you want to change the request URI, you can use uri strip_prefix like so:

route /folder1/* {
   uri strip_prefix /folder1
   reverse_proxy http://localhost:8081
}

In Caddy 2.1, it will probably be simpler, something like:

handle_path /folder1/* {
    reverse_proxy http://localhost:8081
}

(But it’s not finalized yet)

1 Like

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