WebSocket is not working on Caddy v2.1

Hi there,

I’m using Caddy v2.1.
The issue in Chrome Console is:

'wss://10.10.10.10/CustomPath?CustomSession=null' failed: Error during WebSocket handshake: Unexpected response code: 404

And the config in Caddyfile is:

route /CustomPath/* {
  uri strip_prefix /CustomPath
  reverse_proxy websocket:1000/websockethub {
    header_up Sec-WebSocket-Protocol {>Sec-WebSocket-Protocol}
  }
}

What can I do to make websocket to work please

Caddy v2 does not support paths in the proxy address. Instead, add a rewrite rule which will add the path prefix you need before proxying.

Also, it’s unnecessary to do that header_up because in v2 all headers are forwarded by default.

You may also use the new handle_path directive (added in v2.1) instead of route which will let you remove the uri strip_prefix line.

handle_path /CustomPath/* {
  rewrite * /websockethub{path}
  reverse_proxy websocket:1000
}
1 Like

Unfortunately I’m still getting that 404 in the console.

My old config, used with Caddy .9 was:

proxy /CustomPath http://websocket:5100/websockethub {
        without /CustomPath
        websocket
        transparent
        header_upstream Sec-WebSocket-Protocol {>Sec-WebSocket-Protocol}
}

What’s your full Caddyfile at this point? We need the full Caddyfile to make sure that you don’t have something else conflicting with it.

Can you see access logs on your websocket server to see what path it’s seeing as a 404? We’ll need more information to help debug this further.

You can also enable the debug global option which will log more details about the reverse_proxy requests.

It is something like this:

https://
tls internal {
  on_demand
}

encode gzip
log
file_server

header /securepath {
	Access-Control-Allow-Origin *
	-Server
}

@xlsx {
	path *.xlsx
}
header @xlsx Content-Type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


header / -Server ""
header / -x-powered-by ""
header / {
	X-Frame-Options "SAMEORIGIN"
}

route /PathNew/* {
	uri strip_prefix /PathNew
	reverse_proxy pathweb:1123
}
(I have more routes)

handle_path /CustomPath/* {
  rewrite * /websockethub{path}
  reverse_proxy websocket:1000
}

Thanks

Did you enable the debug global option? What do you see in your logs? There’s still not enough info here.

I enabled it like:

log {
  output file /var/log/caddy_test.log
  level DEBUG
}

but I don’t see something logged there

An error log below maybe will help with something

"logger":"http.log.access","msg":"handled request","request":{"method":"GET","uri":"/CustomPath?Session=null","proto":"HTTP/1.1","remote_addr":"10.0.0.2:60434","host":"10.10.10.100","headers":{"Connection":["Upgrade"],"Cache-Control":["no-cache"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"],"Upgrade":["websocket"],"Cookie":["Session=b35ead60-d805-4bd2-87af-abf0008d4a81; SessionInactivityTimeout=0; G4SLogoutUrl=%2F%2F10.10.10.100%2F%2FLogin"],"Sec-Websocket-Extensions":["permessage-deflate; client_max_window_bits"],"Pragma":["no-cache"],"Origin":["https://10.10.10.100"],"Sec-Websocket-Version":["13"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-GB,en-US;q=0.9,en;q=0.8"],"Sec-Websocket-Key":["laxQEQ16zQCszDTzo7zdmQ=="]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"","proto_mutual":true,"server_name":""}},"common_log":"10.0.0.2 - - [06/Jul/2020:09:16:05 +0000] \"GET /CustomPath?Session=null HTTP/1.1\" 404 0","duration":0.000092119,"size":0,"status":404,"resp_headers":{"Server":["Caddy"]}}

Thank you

That enables access logging only, which doesn’t currently have any debug emissions.

You will need to use the debug global option to enable debug logging.

1 Like

The problem has been fixed with:

handle_path /CustomPath/* {
  rewrite * /websockethub?{query}
  reverse_proxy websocket:1000
}

Thank you

3 Likes

Awesome, glad you figured it out! Thanks for sharing the solution.

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