Reverse_proxy only looking to ipv4 loopback

1. The problem I’m having:

http://web.localhost {
	reverse_proxy :5173
}

This reverse proxy seems to only look to 127.0.0.1 (ipv4 loopback). The server I’m attempting to connect to is the vite preview server, which seems to only bind to ipv6:

node    82979 mike   35u  IPv6 0xdc044c12ad1def9      0t0  TCP localhost:5173 (LISTEN)

Caddy refuses to connect to it. It looks like Caddy only looks to ipv4 loopback devices by default (i.e. when a host is not provided, just a port) instead of ipv4 & ipv6 devices. Is this true? I could not find any documentation about this. Could you point me to documentation on which interfaces caddy looks to when creating a connection for a reverse proxy?

I use the following config it works fine:

http://web.localhost {
  reverse_proxy [::1]:5173 {
}

However, this does not find a reverse proxy for an ipv4 loopback. In most situations, you know where the reverse proxy is bound, but this particular case I don’t know exactly which device it is bound to (local aliases for development servers).

2. Error messages and/or full log output:

2024/11/14 18:45:35.570 DEBUG   http.handlers.reverse_proxy     selected upstream       {"dial": ":5173", "total_upstreams": 1}
2024/11/14 18:45:35.570 DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": ":5173", "duration": 0.000152833, "request": {"remote_ip": "127.0.0.1", "remote_port": "56757", "client_ip": "127.0.0.1", "proto": "HTTP/1.1", "method": "GET", "host": "web.localhost", "uri": "/", "headers": {"X-Forwarded-Host": ["web.localhost"], "X-Forwarded-For": ["127.0.0.1"], "User-Agent": ["HTTPie/3.2.3"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "X-Forwarded-Proto": ["http"]}}, "error": "dial tcp :5173: connect: connection refused"}
2024/11/14 18:45:35.570 ERROR   http.log.error.log0     dial tcp :5173: connect: connection refused {"request": {"remote_ip": "127.0.0.1", "remote_port": "56757", "client_ip": "127.0.0.1", "proto": "HTTP/1.1", "method": "GET", "host": "web.localhost", "uri": "/", "headers": {"Connection": ["keep-alive"], "User-Agent": ["HTTPie/3.2.3"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"]}}, "duration": 0.00024375, "status": 502, "err_id": "9uekgh4rb", "err_trace": "reverseproxy.statusError (reverseproxy.go:1269)"}
2024/11/14 18:45:35.570 ERROR   http.log.access.log0    handled request {"request": {"remote_ip": "127.0.0.1", "remote_port": "56757", "client_ip": "127.0.0.1", "proto": "HTTP/1.1", "method": "GET", "host": "web.localhost", "uri": "/", "headers": {"Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"], "User-Agent": ["HTTPie/3.2.3"]}}, "bytes_read": 0, "user_id": "", "duration": 0.00024375, "size": 0, "status": 502, "resp_headers": {"Server": ["Caddy"]}}

3. Caddy version:

❯ caddy --version
v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

brew install caddy

a. System environment:

macOS 15.1 (24B83)

b. Command:

caddy run

c. Service/unit/compose file:

NA

d. My complete Caddy config:

{
	debug
	log {
		output stdout
		format console
		level DEBUG
	}
}

http://web.localhost {
	log {
		output stdout
		format console
		level DEBUG
	}
	reverse_proxy :5173 {
}

5. Links to relevant resources:

Yes, omitting the host is the same as using 127.0.0.1, I think.

I think If you used localhost:5173 then it would likely have worked since your system would resolve localhost to both IPv4 and IPv6 (via the /etc/hosts file) and it could try to dial both.

Anyway, this is just Go stdlib net dialer behaviour, not anything special Caddy is doing.

Also FYI, mind your braces in your config. Remove the { if you don’t have any reverse_proxy subdirectives.

1 Like

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