Can't get socket.io to work behind reverse proxy

1. Caddy version (caddy version):

2.4.6

2. How I run Caddy:

a. System environment:

OSX 12.21 (tested with caddy on its own and the caddy docker image)

b. Command:

caddy run

c. Service/unit/compose file:

-

d. My complete Caddyfile or JSON config:

{
  debug
  auto_https off
}

:80 {
  log {
    output stdout
  }

  route /api/* {
    uri strip_prefix /api
    reverse_proxy localhost:8000
  }

  handle /socket.io/* {
    reverse_proxy localhost:8000
  }

  reverse_proxy localhost:3000
}


3. The problem I’m having:

I am trying to run a create-react-app dev server and a nodejs-koa server behind the reverse proxy.
The nodejs server serves a regular REST-Api as well as a web socket.
The REST-API works fine, however I can’t get the socket to work correctly.
The server logic is mainly handled by the boardgame.io framework (I haven’t worked with sockets before).

The client dispatches multiple socket requests and then immediately closes the connection.
One of the requests gets an invalid message response.
The request after which the connection is being closed, gets three responses: a 2Probe, a 3Probe and then a 5 (without probe).
So as far as I understand this, the socket handshake works fine, but the upgrade from polling to websocket seems to fail somehow.

4. Error messages and/or full log output:

2022/02/23 20:21:49.753 DEBUG   http.handlers.reverse_proxy     upgrading connection     {"upstream": "localhost:8000", "duration": 0.00099606, "r
equest": {"remote_addr": "127.0.0.1:65374", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/socket.io/?EIO=4&transport=websock
et&sid=uT52lBfdW-BzGTu_AAAH", "headers": {"Accept": ["*/*"], "Sec-Websocket-Key": ["KLVFNjK1q2E8AqWXdPPw+w=="], "Dnt": ["1"], "Sec-Fetch-Dest": ["
websocket"], "Sec-Fetch-Mode": ["websocket"], "Accept-Language": ["en-US,de;q=0.7,en;q=0.3"], "Origin": ["http://localhost"], "Connection": ["Upgr
ade"], "Pragma": ["no-cache"], "Upgrade": ["websocket"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Fir
efox/98.0"], "Sec-Websocket-Version": ["13"], "Sec-Websocket-Extensions": ["permessage-deflate"], "Sec-Fetch-Site": ["same-origin"], "Cache-Contro
l": ["no-cache"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["http"], "Accept-Encoding": ["gzip, deflate"]}}}
2022/02/23 20:21:49.707 INFO    http.log.access.log0    handled request {"request": {"remote_addr": "127.0.0.1:65372", "proto": "HTTP/1.1", "metho
d": "GET", "host": "localhost", "uri": "/socket.io/?EIO=4&transport=polling&t=NyeFqLg&sid=uT52lBfdW-BzGTu_AAAH", "headers": {"Referer": ["http://l
ocalhost/match/EMQ4lSS9ugp"], "Sec-Fetch-Site": ["same-origin"], "Cache-Control": ["no-cache"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac
OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0"], "Accept": ["*/*"], "Accept-Encoding": ["gzip, deflate"], "Dnt": ["1"], "Connection": ["keep-al
ive"], "Sec-Fetch-Dest": ["empty"], "Sec-Fetch-Mode": ["cors"], "Pragma": ["no-cache"], "Accept-Language": ["en-US,de;q=0.7,en;q=0.3"]}}, "common_
log": "127.0.0.1 - - [23/Feb/2022:21:21:49 +0100] \"GET /socket.io/?EIO=4&transport=polling&t=NyeFqLg&sid=uT52lBfdW-BzGTu_AAAH HTTP/1.1\" 200 47",
 "user_id": "", "duration": 0.114414626, "size": 47, "status": 200, "resp_headers": {"Date": ["Wed, 23 Feb 2022 20:21:49 GMT"], "Server": ["Caddy"
], "Access-Control-Allow-Origin": ["*"], "Content-Type": ["text/plain; charset=UTF-8"], "Content-Length": ["47"]}}
2022/02/23 20:21:49.858 DEBUG   http.handlers.reverse_proxy     connection closed        {"upstream": "localhost:8000", "duration": 0.00099606, "r
equest": {"remote_addr": "127.0.0.1:65374", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/socket.io/?EIO=4&transport=websock
et&sid=uT52lBfdW-BzGTu_AAAH", "headers": {"Accept": ["*/*"], "Sec-Websocket-Key": ["KLVFNjK1q2E8AqWXdPPw+w=="], "Dnt": ["1"], "Sec-Fetch-Dest": ["
websocket"], "Sec-Fetch-Mode": ["websocket"], "Accept-Language": ["en-US,de;q=0.7,en;q=0.3"], "Origin": ["http://localhost"], "Connection": ["Upgr
ade"], "Pragma": ["no-cache"], "Upgrade": ["websocket"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Fir
efox/98.0"], "Sec-Websocket-Version": ["13"], "Sec-Websocket-Extensions": ["permessage-deflate"], "Sec-Fetch-Site": ["same-origin"], "Cache-Contro
l": ["no-cache"], "X-Forwarded-For": ["127.0.0.1"], "X-Forwarded-Proto": ["http"], "Accept-Encoding": ["gzip, deflate"]}}, "duration": 0.021453445
}
2022/02/23 20:21:49.874 INFO    http.log.access.log0    handled request {"request": {"remote_addr": "127.0.0.1:65374", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/socket.io/?EIO=4&transport=websocket&sid=uT52lBfdW-BzGTu_AAAH", "headers": {"Accept": ["*/*"], "Sec-Websocket-Key": ["KLVFNjK1q2E8AqWXdPPw+w=="], "Dnt": ["1"], "Sec-Fetch-Dest": ["websocket"], "Sec-Fetch-Mode": ["websocket"], "Accept-Language": ["en-US,de;q=0.7,en;q=0.3"], "Origin": ["http://localhost"], "Connection": ["keep-alive, Upgrade"], "Pragma": ["no-cache"], "Upgrade": ["websocket"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0"], "Sec-Websocket-Version": ["13"], "Sec-Websocket-Extensions": ["permessage-deflate"], "Sec-Fetch-Site": ["same-origin"], "Cache-Control": ["no-cache"], "Accept-Encoding": ["gzip, deflate"]}}, "common_log": "127.0.0.1 - - [23/Feb/2022:21:21:49 +0100] \"GET /socket.io/?EIO=4&transport=websocket&sid=uT52lBfdW-BzGTu_AAAH HTTP/1.1\" 0 0", "user_id": "", "duration": 0.277013447, "size": 0, "status": 0, "resp_headers": {"Server": ["Caddy"], "Upgrade": ["websocket"], "Connection": ["Upgrade"], "Sec-Websocket-Accept": ["DvITbtqy+TWQKP8torDITZ+54lM="]}}
2022/02/23 20:21:49.874 INFO    http.log.access.log0    handled request {"request": {"remote_addr": "127.0.0.1:65374", "proto": "HTTP/1.1", "method": "GET", "host": "localhost", "uri": "/socket.io/?EIO=4&transport=websocket&sid=uT52lBfdW-BzGTu_AAAH", "headers": {"Sec-Websocket-Extensions": ["permessage-deflate"], "Sec-Fetch-Site": ["same-origin"], "Cache-Control": ["no-cache"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0"], "Sec-Websocket-Version": ["13"], "Accept-Encoding": ["gzip, deflate"], "Dnt": ["1"], "Sec-Fetch-Dest": ["websocket"], "Sec-Fetch-Mode": ["websocket"], "Accept": ["*/*"], "Sec-Websocket-Key": ["KLVFNjK1q2E8AqWXdPPw+w=="], "Connection": ["keep-alive, Upgrade"], "Pragma": ["no-cache"], "Upgrade": ["websocket"], "Accept-Language": ["en-US,de;q=0.7,en;q=0.3"], "Origin": ["http://localhost"]}}, "common_log": "127.0.0.1 - - [23/Feb/2022:21:21:49 +0100] \"GET /socket.io/?EIO=4&transport=websocket&sid=uT52lBfdW-BzGTu_AAAH HTTP/1.1\" 0 0", "user_id": "", "duration": 0.277013447, "size": 0, "status": 0, "resp_headers": {"Sec-Websocket-Accept": ["DvITbtqy+TWQKP8torDITZ+54lM="], "Server": ["Caddy"], "Upgrade": ["websocket"], "Connection": ["Upgrade"]}}

5. What I already tried:

I tried to figure out a solution by reading this post: I can't get socket.io proxy to work on v2 - #2 by francislavoie
But I was not sure what /path would be in my case (and if I event need this, because the path seems to be fine).
The additional headers also didn’t seem to change anything.

6. Links to relevant resources:

Boardgame.io: GitHub - boardgameio/boardgame.io: State Management and Multiplayer Networking for Turn-Based Games (this is the framework I am using)

Hmm, I’m not seeing a problem with Caddy from those logs, the upstream server must be closing the connection immediately for some reason. Hard to say why. You’ll need to get help from that framework’s authors to see if they can help you debug.

FYI you can simplify this to:

handle_path /api/* {
	reverse_proxy localhost:8000
}

And I’d suggest wrapping your last proxy with a handle as well so that they are properly mutually exclusive

Thanks for the super quick response!

It’s good to know that my proxy config is probably correct. That helps narrowing down the issue.
Also thanks a lot for helping to streamline my config. I just started using caddy yesterday and it has be a very pleasant journey so far :slight_smile:

1 Like

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