Best way to set up reverse proxy for multiple protocols under one subdomain?

I’ve done some searching but couldn’t find much related to my question, but apologies if this has been covered somewhere and I missed it. I’m new to Caddy, but have been experimenting with it the past couple of days and have been mostly successful in working with it so far. However, I’m wondering what the best way to go about setting up a reverse proxy that would handle different protocols under one domain entry.

For instance, let’s say I’m running zwavejs2mqtt. This particular service has a web UI accessible on port 8091, and a websocket connection accessible on port 3023. Is there a way to have a single subdomain handle both? So if I were to go to https://zwjs.example.com I’d get the web UI, but ws://zwjs.example.com would connect to the websocket?

Thanks in advance.

1. Caddy version (caddy version):

v2.4.0-beta.1

2. How I run Caddy:

a. System environment:

Docker version 20.10.3 on Ubuntu 20.04.2 LTS

b. Command:

docker-compose up

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

paste config here, replacing this text
use `caddy fmt` to make it readable
DO NOT REDACT anything except credentials
or helpers will be sad

3. The problem I’m having:

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

Pretty simple, actually:

zwjs.example.com {
	@ws {
		header Connection *Upgrade*
		header Upgrade websocket
	}
	handle @ws {
		reverse_proxy websockets:3023
	}

	handle {
		reverse_proxy ui:8091
	}
}

That’s it :slight_smile:

Makes use of named matchers to detect the initial websocket request, which must have those two headers, and routes it appropriately. Everything else will fall through to the other handle block.

This works specifically because websockets is initially an HTTP request which gets upgraded to a two-way TCP pipe. If it wasn’t HTTP, then Caddy wouldn’t be able to do it (but you could use the caddy-l4 plugin possibly, depending on your needs, but that’s another story).

2 Likes

Oh, wow, that is simple, haha. Thank you for the helpful explanation and the doc links. I’ll check those out, too. Much appreciated!

1 Like

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