I can't get socket.io proxy to work on v2

You were on the right track!

In Caddy v2, path matching is exact-match, so you must specify * to allow any subpaths. /path will only allow /path and not /path/foo.

If you use /path*, it will work, but it will also allow /pathfoo. You could also do a redirect or rewrite from /path to /path/ and use /path/* if that matters for you.

Specifying {uri} there is incorrect, a proxy dial address is just the host/port to connect to. Caddy v2 doesn’t support paths in proxy addresses unlike v1, because it adds a bunch of complications. Instead, you can rewrite the URL before proxying to have the prefix you need.

Also, header_up X-Forwarded-For {remote} is not necessary, Caddy v2 does this for you already (and has slightly more correct logic to handle the case where you might have multiple proxies chained). The other two are likely not necessary either, I recommend you try removing them to see if it works without them. If your proxied service handles X-Forwarded-For, then X-Real-IP is not necessary, and the Host is already passed through transparently from the initial request, so it’s also likely unnecessary.

So with all that said, I think you’d want something like this (the rewrite/matcher is up to you to change if you care)

rewrite /path /path/
handle /path/* {
	uri strip_prefix /path
	rewrite * /socket.io{path}
	reverse_proxy localhost:6969 {
		header_up Host {host}
		header_up X-Real-IP {remote}
	}
}

FYI, in Caddy v2.1 (beta 1 is already out if you want to try it), you could use handle_path instead and remove the uri strip_prefix line – handle_path will be a shortcut to that construct.

I use handle here to isolate the rewrites from the rest of your config so that they don’t affect other services/directives, and it avoids needing to repeat the same /path/* matcher all over.

3 Likes