Handle_response causes "The requested address is not valid in its context."

1. The problem I’m having:

I’m trying to implement a scenario where my upstream backend returns an S3 object URL in X-Accel-Redirect headers. I’m trying to have Caddy proxy the S3 object by using the handle_response directive, as follows:

handle_response @accel {
	rewrite * {rp.header.X-Accel-Redirect-Path}
	reverse_proxy {rp.header.X-Accel-Redirect-Host} {
		header_up Host {rp.header.X-Accel-Redirect-Host}
	}
	method * GET
}

A call that would result in a response matching @accel however fails.

I also tried using reverse_proxy {rp.header.X-Accel-Redirect} where this one header contained the full URL but this caused a different error (parsing the port number).

2. Error messages and/or full log output:

Error: dial tcp [::1]:0: connectex: The requested address is not valid in its context.

2024/10/25 21:27:49.101 ERROR   http.log.error.log0     dial tcp [::1]:0: connectex: The requested address is not valid in its context. {"request": {"remote_ip": "::1", "remote_port": "62662", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8080", "uri": "/api/files/2", "headers": {"Connection": ["keep-alive"], "User-Agent": ["PostmanRuntime/7.42.0"], "Postman-Token": ["11d1b592-b0b1-4de3-9fa8-6afb4dad48a1"], "Accept-Encoding": ["gzip, deflate, br"], "Authorization": [], "Accept": ["*/*"], "Cache-Control": ["no-cache"]}}, "duration": 0.0148386, "status": 502, "err_id": "8g87km17q", "err_trace": "reverseproxy.statusError (reverseproxy.go:1299)"}
2024/10/25 21:27:49.102 ERROR   http.log.access.log0    handled request {"request": {"remote_ip": "::1", "remote_port": "62662", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8080", "uri": "/api/files/2", "headers": {"Authorization": [], "Accept": ["*/*"], "Cache-Control": ["no-cache"], "Accept-Encoding": ["gzip, deflate, br"], "Connection": ["keep-alive"], "User-Agent": ["PostmanRuntime/7.42.0"], "Postman-Token": ["11d1b592-b0b1-4de3-9fa8-6afb4dad48a1"]}}, "user_id": "", "duration": 0.0148386, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"]}}

3. Caddy version:

2.6.4

4. How I installed and ran Caddy:

a. System environment:

Windows 11, AMD64

b. Command:

caddy run --config .\Caddyfile-dev

c. Service/unit/compose file: n/a

d. My complete Caddy config:

http://localhost:8080

handle_path /api/* {
	rewrite * /api{path}
	reverse_proxy localhost:8090  {
		@accel {
			header X-Accel-Redirect-Host *
			header X-Accel-Redirect-Path *
		}
		handle_response @accel {
			rewrite * {rp.header.X-Accel-Redirect-Path}
			reverse_proxy {rp.header.X-Accel-Redirect-Host} {
				header_up Host {rp.header.X-Accel-Redirect-Host}
			}
			method * GET
		}
	}
}
handle_path /auth/* {
	reverse_proxy h2c://localhost:9000
}
handle_path /* {
	reverse_proxy localhost:8091
}
log {
	level debug
}

That’s a pretty old version. Please use v2.8.4.

Please enable the debug global option and show your full logs.

2 Likes

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