Websocket "'502 Bad Gateway' caused by: dial tcp connect: connection refused"

I'm trying to make a websocket application work on Traefik 2.3, but when I send request to traefik I get 502 as following.

time="2021-01-27T10:08:17Z" level=debug msg="vulcand/oxy/roundrobin/rr: begin ServeHttp on request" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/json\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\",\"RawFragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Connection\":[\"Upgrade\"],\"Origin\":[\"http://localhost:4443\"],\"Sec-Websocket-Extensions\":[\"permessage-deflate; client_max_window_bits\"],\"Sec-Websocket-Key\":[\"nbMGA+vLk//P91QRwUi5rQ==\"],\"Sec-Websocket-Version\":[\"13\"],\"Upgrade\":[\"websocket\"],\"X-Forwarded-Host\":[\"localhost:4443\"],\"X-Forwarded-Port\":[\"4443\"],\"X-Forwarded-Proto\":[\"https\"],\"X-Forwarded-Server\":[\"26d62bbceae5\"],\"X-Real-Ip\":[\"172.19.0.1\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"localhost:4443\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"172.19.0.1:57154\",\"RequestURI\":\"/json\",\"TLS\":null}"
time="2021-01-27T10:08:17Z" level=debug msg="vulcand/oxy/roundrobin/rr: Forwarding this request to URL" ForwardURL="http://9cc6620bf077:4441" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/json\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\",\"RawFragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Connection\":[\"Upgrade\"],\"Origin\":[\"http://localhost:4443\"],\"Sec-Websocket-Extensions\":[\"permessage-deflate; client_max_window_bits\"],\"Sec-Websocket-Key\":[\"nbMGA+vLk//P91QRwUi5rQ==\"],\"Sec-Websocket-Version\":[\"13\"],\"Upgrade\":[\"websocket\"],\"X-Forwarded-Host\":[\"localhost:4443\"],\"X-Forwarded-Port\":[\"4443\"],\"X-Forwarded-Proto\":[\"https\"],\"X-Forwarded-Server\":[\"26d62bbceae5\"],\"X-Real-Ip\":[\"172.19.0.1\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"localhost:4443\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"172.19.0.1:57154\",\"RequestURI\":\"/json\",\"TLS\":null}"
time="2021-01-27T10:08:17Z" level=debug msg="'502 Bad Gateway' caused by: dial tcp 172.19.0.3:4441: connect: connection refused"
time="2021-01-27T10:08:17Z" level=debug msg="vulcand/oxy/roundrobin/rr: completed ServeHttp on request" Request="{\"Method\":\"GET\",\"URL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"/json\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\",\"RawFragment\":\"\"},\"Proto\":\"HTTP/1.1\",\"ProtoMajor\":1,\"ProtoMinor\":1,\"Header\":{\"Connection\":[\"Upgrade\"],\"Origin\":[\"http://localhost:4443\"],\"Sec-Websocket-Extensions\":[\"permessage-deflate; client_max_window_bits\"],\"Sec-Websocket-Key\":[\"nbMGA+vLk//P91QRwUi5rQ==\"],\"Sec-Websocket-Version\":[\"13\"],\"Upgrade\":[\"websocket\"],\"X-Forwarded-Host\":[\"localhost:4443\"],\"X-Forwarded-Port\":[\"4443\"],\"X-Forwarded-Proto\":[\"https\"],\"X-Forwarded-Server\":[\"26d62bbceae5\"],\"X-Real-Ip\":[\"172.19.0.1\"]},\"ContentLength\":0,\"TransferEncoding\":null,\"Host\":\"localhost:4443\",\"Form\":null,\"PostForm\":null,\"MultipartForm\":null,\"Trailer\":null,\"RemoteAddr\":\"172.19.0.1:57154\",\"RequestURI\":\"/json\",\"TLS\":null}"

My route is

[http.routers]
  [http.routers.wss-server-server]
    rule = "Host(`localhost`)"
    service = "wss-server"
	middlewares = [
       "midleware-chain-websocket"
    ]
    tls=true
	entrypoints = [
      "wss"
    ]
  
[http.services]
  [http.services.wss-server.loadBalancer]
	passHostHeader = false
    [[http.services.wss-server.loadBalancer.servers]]
      url  = "http//:myserverwss:4441"
    

my midleware only put X-forward

  [http.middlewares.midleware-chain-websocket.chain]
    middlewares = ["websocket-midleware-headers"]

  [http.middlewares.websocket-midleware-headers.headers.customRequestHeaders]
	X-Forwarded-Proto = ["https"]

The server you have defined is refusing the connection. Investigate why that service is refusing connections.

when I use a client socket outside of traefik it works fine, maybe my configuration in traefik is not correct.

The same client cannot connect when I use the traefik route.

This says the connection is being actively refused. Does this server required extra configuration. Is it actually bound on the referenced ip address. Does it have its own logs to see what happens to the connection.

Standalone is it using https? You service url is plain http.

It was an attempt.

I put the websocket server in trace log mode, but nothing. I'll try another way, but I have no idea

1 Like

Hello @haikalrios , did you find a solution to your problem ? I seem to be hitting the same issue on my docker-compose projects (cf. 502 Bad Gateway' caused by: dial tcp 172.30.0.8:29091: connect: connection refused" )