Unable to execute multiple redirects based on response status with reverse proxies

1. The problem I’m having:

My caddy server is a mini PC, which is trying to create a reverse proxy for a locally hosted Ollama instance in another PC, but I want any error response (typically due to sleeping PC to conserve power, status codes 5xx ?) to be redirected to reverse proxy another URL (local to mini PC), which is controlled by a python/docker script wol_api and responds appropriately if it manages to wake up the Ollama PC. After successfully waking up the PC, redirect back to original request URL and then send response.

Basically, I don’t see any WoL nor redirects taking place and get my custom error message. Not sure where I went wrong (likely multiple places).

2. Error messages and/or full log output:

2025/05/02 20:47:01.728	DEBUG	http.handlers.reverse_proxy	selected upstream	{"dial": "ollama-pc-lan-addr:11434", "total_upstreams": 1}
2025/05/02 20:47:04.731	DEBUG	http.handlers.reverse_proxy	upstream roundtrip	{"upstream": "ollama-pc-lan-addr:11434", "duration": 3.002812458, "request": {"remote_ip": "mini-pc-addr", "remote_port": "64255", "client_ip": "mini-pc-addr", "proto": "HTTP/1.1", "method": "POST", "host": "mini-pc:11434", "uri": "/api/generate", "headers": {"X-Forwarded-Host": ["mini-pc:11434"], "Via": ["1.1 Caddy"], "Content-Length": ["63"], "Content-Type": ["application/x-www-form-urlencoded"], "User-Agent": ["curl/8.7.1"], "Accept": ["*/*"], "X-Forwarded-For": ["mini-pc-addr"], "X-Forwarded-Proto": ["http"]}}, "error": "dial tcp ollama-pc-lan-addr:11434: i/o timeout"}
2025/05/02 20:47:04.731	DEBUG	http.log.error.log0	dial tcp ollama-pc-lan-addr:11434: i/o timeout	{"request": {"remote_ip": "mini-pc-addr", "remote_port": "64255", "client_ip": "mini-pc-addr", "proto": "HTTP/1.1", "method": "POST", "host": "mini-pc:11434", "uri": "/api/generate", "headers": {"Accept": ["*/*"], "Content-Length": ["63"], "Content-Type": ["application/x-www-form-urlencoded"], "User-Agent": ["curl/8.7.1"]}}, "duration": 3.005034, "status": 502, "err_id": "apdeutbpd", "err_trace": "reverseproxy.statusError (reverseproxy.go:1390)"}
2025/05/02 20:47:04.731	ERROR	http.log.access.log0	handled request	{"request": {"remote_ip": "mini-pc-addr", "remote_port": "64255", "client_ip": "mini-pc-addr", "proto": "HTTP/1.1", "method": "POST", "host": "mini-pc:11434", "uri": "/api/generate", "headers": {"Content-Type": ["application/x-www-form-urlencoded"], "User-Agent": ["curl/8.7.1"], "Accept": ["*/*"], "Content-Length": ["63"]}}, "bytes_read": 0, "user_id": "", "duration": 3.005034, "size": 32, "status": 502, "resp_headers": {"Server": ["Caddy"], "Content-Type": ["text/plain; charset=utf-8"]}}

3. Caddy version:

v2.10.0 h1:fonubSaQKF1YANl8TXqGcn4IbIRUDdfAkpcsfI/vX5U=

4. How I installed and ran Caddy:

a. System environment:

MacOS 15.4.1 (24E263)
Using brew install

b. Command:

user@mini-pc : which caddy
/opt/homebrew/bin/caddy
user@mini-pc : caddy run --config /opt/homebrew/etc/Caddyfile

Currently testing on mini-pc itself, using wol_api separately via curl works (to ensure it isn’t the issue)

c. Service/unit/compose file:

Not using brew service/docker

d. My complete Caddy config:

{
	debug
	auto_https off
}

# NOTE: Values have been changed for privacy, WoL service runs on 28080
http://mini-pc-addr:11434 {
	# MAC Address of PC
	@mac path /wol/FF:FF:FF:FF:FF:FF
	
	# Strip custom path and initiate WoL
	handle_path /wakeup/* {
		reverse_proxy http://127.0.0.1:28080 {
			@success status 2xx
			handle_response @success {
				uri strip_prefix @mac
				redir {uri}
			}
		}
	}

	reverse_proxy http://ollama-pc-lan-addr:11434 {
		@errors status 4xx 5xx
		handle_response @errors {
			redir http://mini-pc-addr:11434/wakeup/@mac?{url}
		}
	}

	handle_errors {
		respond "Unknown error from {uri}"
	}

	log {
		output stdout
	}
}

5. Links to relevant resources:

I think caddy might also not be how I resolve this problem, since it is typically for web page redirections and simple app reverse proxying.

Closing this as I circumvented the issue by using caddy-exec package to generate wol requests (and for completeness if anyone else stumbles across, basically route the reverse proxy’s handle response for 5xx error code to a routed exec (wol) and then reverse proxy the original destination).