V2: Dynamic Reverse Proxy to sub folders

Hey folks,

I’m looking at using Caddy V2 for a project. A requirement that I have to dynamic reverse proxy to another domain path, for example:

userdomain.com -> myproject.com/userdomain.com

where I don’t necessarily know what the userdomain.com will be. I’d like the reverse proxy to automatically route any domain pointed at the caddy service in the same way.

Is this possible? If so can I get some explanation how?

(Apologies if I got some terminology incorrect, I’m fairly new to this area and Caddy)

UPDATE:
Thanks to @matt’s reply below, I wrote out a solution that looks like:

{
	"apps": {
		"http": {
			"servers": {
				"example": {
					"listen": ["127.0.0.1:5000"],
					"routes": [
						{
							"handle": [
                                {
                                    "handler": "rewrite",
                                    "method": "GET",
                                    "uri": "/{http.request.hostport}",
                                    "rehandle": true
                                },
								{
									"handler": "reverse_proxy",
									"upstreams": [
										{
											"dial": "localhost:4444"
										}
									]
								}
							]
						}
					],
					"automatic_https":{
						"disable": true
					}
				}
			}
		}
	}
}

Thanks for trying Caddy 2 while it’s still in beta!

For now, I’d recommend using the JSON config while we flush out the rest of the v2 Caddyfile to support your use case. (It might already, but I’d be surprised, and I haven’t looked at it in a while…)

First you’d want to rewrite the request to append the host to the path: Home · caddyserver/caddy Wiki · GitHub

Then proxy to your upstream: Home · caddyserver/caddy Wiki · GitHub

The Caddyfile will support what you want eventually, but we’re not quite there yet.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.