【问题标题】:Handle Socket.IO requests through Nginx reverse proxy通过 Nginx 反向代理处理 Socket.IO 请求
【发布时间】:2020-07-22 09:03:31
【问题描述】:

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

看来 Socket.IO 客户端连接到后端没有问题,因为它获得了 pongundefined 数据和以下响应:

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

更新 1

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

更新 2

这里是 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

更新 3

我通过将 Socket.IO 路径更改为 / 进行了测试,但没有成功,我得到了相同的结果。

更新 4

我将 Socket.IO 客户端传输更改为 polling 进行了测试,但没有成功,我得到了相同的结果。

【问题讨论】:

    标签: nginx websocket socket.io


    【解决方案1】:

    我解决了这个问题。就我而言,这是一个损坏的 JWT 机密,并且日志也说明了这一点(查看 invalid signature 部分)。由于GitLab issue,我的 JWT 机密已损坏。

    【讨论】:

      【解决方案2】:

      您的连接似乎已被 nginx 自动关闭。您是否在 nginx 中设置了 WebSocket 支持?如果不按照此链接中的说明进行操作:https://www.nginx.com/blog/websocket-nginx/

      要验证这是导致问题的原因:尝试当 socket.io 连接模式设置为仅轮询时您的应用程序是否工作。

      【讨论】:

      • 我正在使用nginx-proxy,它似乎可以正确处理 Nginx 配置,正如您在我的问题中所写的101 Switching Protocols 的回复中看到的那样。
      猜你喜欢
      • 2016-01-02
      • 2018-05-07
      • 1970-01-01
      • 2021-12-01
      • 2013-07-16
      • 2019-06-20
      • 1970-01-01
      • 2019-09-27
      • 2014-05-20
      相关资源
      最近更新 更多