The original post: /r/nginx by /u/ctrtanc on 2024-09-06 03:36:13.
I have a server that I've written to listen on port 8500 for websockets. I have a local dns lookup through my pi-hole (not on the same raspberry pi) that resolves rpi4b.mc
to the local ip address of the raspberry pi. This is working fine when I run nslookup on that hostname. I have minecraft running on my pc, and I'm using the command /wsserver rpi4b.mc/ws
to attempt to connect to the raspberry pi server websocket.
If I run /wsserver rpi.local:8500
it connects without issue and everything is good. If I use yarn dlx wscat --connect rpi4b.mc/ws
from my computer, that connects and everything is good, so both the reverse proxy and the dns resolution seem to be working fine. However, when I run /wsserver rpi4b.mc/ws
it fails to connect and throws an error on the server. I cannot for the life of me figure out why it's acting this way. It seems that the reverse proxy is working for some requests and not for others, even when they come from the same machine. Any help/insight is appreciated. Thanks!
The error I get on the server is:
RangeError: Invalid WebSocket frame: invalid status code 59907 at Receiver.controlMessage (/<filepath>/.yarn/__virtual__/ws-virtual-ac79615cae/3/.yarn/berry/cache/ws-npm-8.18.0-56f68bc4d6-10c0.zip/node_modules/ws/lib/receiver.js:626:30) at Receiver.getData (/<filepath>/.yarn/__virtual__/ws-virtual-ac79615cae/3/.yarn/berry/cache/ws-npm-8.18.0-56f68bc4d6-10c0.zip/node_modules/ws/lib/receiver.js:477:12) at Receiver.startLoop (/<filepath>/.yarn/__virtual__/ws-virtual-ac79615cae/3/.yarn/berry/cache/ws-npm-8.18.0-56f68bc4d6-10c0.zip/node_modules/ws/lib/receiver.js:167:16) at Receiver._write (/<filepath>/.yarn/__virtual__/ws-virtual-ac79615cae/3/.yarn/berry/cache/ws-npm-8.18.0-56f68bc4d6-10c0.zip/node_modules/ws/lib/receiver.js:94:10) at writeOrBuffer (node:internal/streams/writable:570:12) at _write (node:internal/streams/writable:499:10) at Writable.write (node:internal/streams/writable:508:10) at Socket.socketOnData (/<filepath>/.yarn/__virtual__/ws-virtual-ac79615cae/3/.yarn/berry/cache/ws-npm-8.18.0-56f68bc4d6-10c0.zip/node_modules/ws/lib/websocket.js:1355:35) at Socket.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) { code: 'WS_ERR_INVALID_CLOSE_CODE', [Symbol(status-code)]: 1002 }
Nginx debug logs are:
2024/09/05 21:00:25 [debug] 33556#33556: accept on 0.0.0.0:80, ready: 0 2024/09/05 21:00:25 [debug] 33556#33556: posix_memalign: 000000557F572EB0:512 @16 2024/09/05 21:00:25 [debug] 33556#33556: *63 accept: <minecraftip>:<port> fd:3 2024/09/05 21:00:25 [debug] 33556#33556: *63 event timer add: 3: 60000:451500109 2024/09/05 21:00:25 [debug] 33556#33556: *63 reusable connection: 1 2024/09/05 21:00:25 [debug] 33556#33556: *63 epoll add event: fd:3 op:1 ev:80002001 2024/09/05 21:00:25 [debug] 33556#33556: epoll del event: fd:5 op:2 ev:00000000 2024/09/05 21:00:25 [debug] 33556#33556: epoll add event: fd:5 op:1 ev:10000001 2024/09/05 21:00:25 [debug] 33556#33556: *63 http wait request handler 2024/09/05 21:00:25 [debug] 33556#33556: *63 malloc: 000000557F575700:1024 2024/09/05 21:00:25 [debug] 33556#33556: *63 recv: eof:0, avail:-1 2024/09/05 21:00:25 [debug] 33556#33556: *63 recv: fd:3 149 of 1024 2024/09/05 21:00:25 [debug] 33556#33556: *63 reusable connection: 0 2024/09/05 21:00:25 [debug] 33556#33556: *63 posix_memalign: 000000557F589710:4096 @16 2024/09/05 21:00:25 [debug] 33556#33556: *63 http process request line 2024/09/05 21:00:25 [debug] 33556#33556: *63 http request line: "GET /ws HTTP/1.1" 2024/09/05 21:00:25 [debug] 33556#33556: *63 http uri: "/ws" 2024/09/05 21:00:25 [debug] 33556#33556: *63 http args: "" 2024/09/05 21:00:25 [debug] 33556#33556: *63 http exten: "" 2024/09/05 21:00:25 [debug] 33556#33556: *63 posix_memalign: 000000557F56F9F0:4096 @16 2024/09/05 21:00:25 [debug] 33556#33556: *63 http process request header line 2024/09/05 21:00:25 [debug] 33556#33556: *63 http header: "Upgrade: websocket" 2024/09/05 21:00:25 [debug] 33556#33556: *63 http header: "Connection: Upgrade"
This is the basic server setup:
import { WebSocketServer } from 'ws';
const PORT = process.env.WS\_SERVER\_PORT || 8500;
const wss = new WebSocketServer({ port: PORT });
wss.on("listening", () => console.log(`Listening [${PORT}]`));
wss.on("error", console.error);
wss.on("wsClientError", console.error);
wss.on("open", () => {
wss.send("WELCOME ONE AND ALL!!");
});
wss.on("connection", (socket) => {
console.log("user connected");
socket.on("error", console.error); socket.on("message", data => { try { // parsing the data and stuff } catch (error) { console.error(error); } });
});
I have nginx set up with this conf file:
map $http\_upgrade $connection\_upgrade {
default upgrade;
'' close;
}
upstream mc\_wss {
server 127.0.0.1:8500;
}
server {
listen 80;
listen 443;
server_name rpi4b.mc;
access_log /var/log/nginx/rpi4b.mc.access.log; error_log /var/log/nginx/rpi4b.mc.error.log;
location /ws { proxy_pass http://mc_wss;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
#proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 3600s;
}
}