Dynamic reverse proxy with strip_prefix

1. Caddy version (caddy version): v2.4.5

2. How I run Caddy:

caddy run

a. System environment:

Mac

My complete Caddyfile or JSON config:

localhost:2015

handle {
	uri strip_prefix /{http.request.uri.path.0}
	reverse_proxy {
		to www.{http.request.uri.path.0}:443
		header_up Host {http.reverse_proxy.upstream.host}
		transport http {
			tls
		}
	}
}

3. The problem I’m having:

I am trying to dynamically reverse-proxy a url like localhost:2015/google.com/robots.txt to www.google.com/robots.txt

Any ideas on how I can go about it?

4. What I already tried:

I tried the above Caddyfile but it rewrites the path before sending upstream.

Sounds like what you want is a forward proxy, not a reverse proxy. What you’re trying is really weird for reverse_proxy.

There is a forwardproxy Caddy plugin, but support for v2 isn’t complete, I think. (/cc @matt Support Caddy 2 by mholt · Pull Request #74 · caddyserver/forwardproxy · GitHub)

That said, you could probably use a path_regexp matcher to extract the part of the URL you want before stripping the prefix, then using the regexp capture group, which’ll be preserved after stripping.

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