Reverse proxy only websockets

How can I use both the file server and reverse proxy, such that regular HTTP requests can be made to get files (./client), but any websocket traffic (only) is proxied back to a websocket server I have running locally (127.0.0.1:4567)?

Whenever I try running both at once, any HTTP requests also get proxied, which is undesired.

Running the file server alone serves files fine, but I need to proxy a WS connection too.

I can’t find any clarification about how HTTP/WS are differentiated in the docs.

Caddyfile

localhost, dungeonz.io

root * ./client
file_server

reverse_proxy 127.0.0.1:4567

You use request matchers for this:

There’s even an example in there for matching websocket requests!

@websockets {
	header Connection *Upgrade*
	header Upgrade    websocket
}
reverse_proxy @websockets 127.0.0.1:4567

Oh, cool. How did I miss that, thanks. Works now.

1 Like

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