通过Nginx反向代理处理Socket.IO请求

时间:2020-07-22 09:03:31

标签: nginx websocket socket.io

我的后端在开发过程中完美地处理了API请求和Socket.IO请求。在生产中,我的后端仍然可以完美地处理API请求,但不响应Socket.IO请求。

似乎Socket.IO客户端毫无问题地连接到后端,因为它获得了pong的{​​{1}}数据和以下响应:

undefined

更新1

我尝试使用Echo Test连接到后端,并且连接成功。

更新2

这是Socket.IO客户端日志:

HTTP/1.1 101 Switching Protocols
Server: nginx/1.17.6
Date: Wed, 22 Jul 2020 08:50:51 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: +XXXXxxXxxX/xxxxXXXxxxxx/Xx=
Sec-WebSocket-Extensions: permessage-deflate
Strict-Transport-Security: max-age=31536000

更新3

我通过将Socket.IO路径更改为socket.io-client:url parse https://staging.backend.xxx.us/dashboard socket.io-client new io instance for https://staging.backend.xxx.us/dashboard socket.io-client:socket emitting packet with ack id 0 socket.io-client:socket emitting packet with ack id 1 socket.io-client:socket emitting packet with ack id 2 socket.io-client:manager readyState closed socket.io-client:manager opening https://staging.backend.xxx.us/dashboard engine.io-client:socket creating transport "websocket" engine.io-client:socket setting transport websocket socket.io-client:manager connect attempt will timeout after 20000 engine.io-client:socket socket receive: type "open", data "{"sid":"fk3DbI3H-0qXRZEAAAAU","upgrades":[],"pingInterval":25000,"pingTimeout":5000}" engine.io-client:socket socket open socket.io-client:manager open socket.io-client:manager cleanup socket.io-client:socket transport is open - connecting socket.io-client:manager writing packet {"type":0,"nsp":"/dashboard"} socket.io-parser encoding packet {"type":0,"nsp":"/dashboard"} socket.io-parser encoded {"type":0,"nsp":"/dashboard"} as 0/dashboard, engine.io-client:socket flushing 1 packets in socket engine.io-client:socket socket receive: type "message", data "0" socket.io-parser decoded 0 as {"type":0,"nsp":"/"} engine.io-client:socket socket receive: type "message", data "4/dashboard,"invalid signature"" socket.io-parser decoded 4/dashboard,"invalid signature" as {"type":4,"nsp":"/dashboard","data":"invalid signature"} engine.io-client:socket writing ping packet - expecting pong within 5000ms engine.io-client:socket flushing 1 packets in socket engine.io-client:socket socket receive: type "pong", data "undefined" engine.io-client:socket socket close with reason: "transport close" socket.io-client:manager onclose socket.io-client:manager cleanup socket.io-client:socket close (transport close) socket.io-client:manager will wait 690ms before reconnect attempt socket.io-client:manager attempting reconnect 进行了测试,但是没有用,并且得到了相同的结果。

更新4

我进行了测试,将Socket.IO客户端传输更改为/,但是没有用,并且得到了相同的结果。

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。就我而言,这是一个损坏的JWT机密,日志中也对此进行了说明(请参阅invalid signature部分)。由于GitLab issue,我的JWT机密已损坏。

答案 1 :(得分:0)

似乎您的连接已被nginx自动关闭。您是否在Nginx中设置了WebSocket支持?如果未遵循,请遵循以下链接上的说明:https://www.nginx.com/blog/websocket-nginx/

要验证这是导致问题的原因,请执行以下操作:当socket.io连接模式设置为仅轮询时,尝试应用程序是否正常工作。