Reverse_proxy.rewrite why not support <matcher> only <to>

1. Output of v2.6.2:

2. How I run Caddy:

a. System environment:

Win11 x64

b. Command:

caddy fmt --overwrite
caddy run --config ./Caddyfile

c. Service/unit/compose file:

none

d. My complete Caddy config:

http://localhost:5200 {
	@postandget {
		method POST PUT GET
		path /test/*
	}
	reverse_proxy @postandget {
                # why not the rewrite  not support  <matcher> only <to>
                # i want http://localhost:5203/test/* => http://localhost:5203/*
		rewrite  ????
		header_up host {http.reverse_proxy.upstream.host}
		to http://localhost:5203 http://localhost:5204
	}
}

3. The problem I’m having:

i want to change url from http://localhost:5203/test/* => http://localhost:5203/*
i dont want to use handle or uri or route,they make the code looks complicated.

Is there any other simpler way to make this work?

handle_path is the simplest way.

Your config could use some cleanup though.

http://localhost:5200 {
	handle_path /test/* {
		reverse_proxy localhost:5203 localhost:5204
	}
}

That’s all you need.

Keep in mind that any request to other paths will not be handled by this config, and you should write additional handle blocks to match or catch those requests.

I’d recommend using subdomains instead of subpaths, in general. For example, you can use test.localhost as a domain. Modern browsers all resolve *.localhost to 127.0.0.1 or ::1 without you having to do anything extra; other clients might not though so you might need to update your /etc/hosts or whatever to allow it. YMMV.

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