Websocket Proxying gives 502

First of all thank you for this amazing piece of software you’ve written. I had been looking for such a thing for some time and yesterday just stumbled on to your server from Lets Encrypt’s site link. Tried it, loved it and now I’m converting our nginx configs to caddy. All was good until I ran into a websocket and there’s where our story begins:

What am I trying to do:
Proxying a simple websocket connection using a sample go server: github: go-websocket-sample/master/server.go

What I’ve already tried
Other than researching this for the past 24 hours, going through documentation, various examples under caddyserver repo, and searching this forum / github issues, stackoverflow questions, I’m not sure how to answer this question. I’ve reduced the problem down the absolute minimal Cadyfile:

http://localhost:4051 {
    proxy /wstime ws://localhost:4050/wstime {
        websocket
    }
}

where localhost:4050/wstime is the sample server linked above that pushes timestamps on connect and works flawlessly on its own.

I’m using https://www.websocket.org/echo.html client connecting to ws://localhost:4050/wstime verifying that server is responding. However, switching it to ws://localhost:4051/wstime returns 502 instead.

Error information returned in chrome:
WebSocket connection to ‘ws://localhost:4051/wstime’ failed: Error during WebSocket handshake: Unexpected response code: 502

Environment Information:

OS: Windows 10 Creators Update / Gentoo Linux
Go version: 1.8.1
Caddy version: 0.10.0, 0.10.2, master/HEAD @ 958abcf – v0.10.2-9-g958abcf (tried various versions in case there was a bug fix I don’t have)

How did I run caddy: caddy (just plain exe picking Caddyfile from current folder)

If i’ve missed something, please feel free to ask.

I don’t think websocket is part of the proxy directive.

Documentation suggests otherwise @Fastidious: https://caddyserver.com/docs/proxy#presets

Though I have tried without it as well and result is same.

Indeed! I got confused with https://caddyserver.com/docs/websocket.

Can you try this:

http://localhost:4051 {
    proxy /wstime {
        websocket
    }
}

that returns an error I’m afraid (and I can understand why - it doesn’t know where to send proxy to):

Caddyfile:4 - Parse error: Wrong argument count or unexpected line ending after ‘}’

That won’t work, proxy requires another argument: a destination to proxy to.

Not sure if this will work for your situation, but the way I do it is…

Www.example.com {
Proxy /api/v1/streaming http://localhost:4000/ {
Websocket
}
}

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