[solved] socket.io web socket proxy with root pages

Awhile back I posted this https://caddy.community/t/how-to-pass-through-two-websockets-to-a-spa-using-root

It is now a locked topic but I wanted to post on my actual solution how to proxy an underlying websocket with a regularly served root.

What I discovered is that socket.io the websocket client my case by default uses /socket.io as the path for its web socket client. So I only needed to add a proxy for that path using http not ws. socket.io also allows a custom path option so one could set that and use it below but the default works fine.
https://socket.io/docs/client-api/#With-custom-path. Of note if you aren’t using tls on that server then at least in Firefox you will get a security error which for firefox can be fixed in the browser or alternatively by making your socket.io server use certificates (even self signed ones) https/wss. https://stackoverflow.com/questions/11768221/firefox-websocket-security-issue/12042843#12042843

https://lights.xxxx.net {
        import wildcard_cert
        root /mnt/data/webs/lights-frontend
        proxy /socket.io http://10.0.0.115:3031 {
          websocket
          transparent
          }
        }
1 Like