gpt4 book ai didi

node.js - Socket.io 根据 http 请求断开连接

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:04 25 4
gpt4 key购买 nike

我正在 AWS EC2 实例上为我的 node.js 应用程序运行 nginx。我想使用 websockets (socket.io) 和普通的 http 请求/响应。我的问题是,每当我从移动设备到服务器有一个事件的套接字连接并尝试发出正常的 http 请求时,就会调用移动设备的 socket.io 错误函数,并显示消息“502 Bad Gateway”。

只有套接字有效。只有普通的 http 请求才有效。

我发现,这个问题是在我将 nginx 设置为仅使用 https 后发生的。

这是我在/sites-enabled/sites-available 中的 nginx 配置:

server {
listen 80;
listen 443 ssl;

server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;

if ($scheme != "https") {
rewrite ^ https://$host$request_uri? permanent;
}

location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}

Nginx 错误日志:

[error] 14117#14117: *50 upstream prematurely closed connection while reading response header from upstream, client: 78.94.9.226, server: example.com, request: "GET /socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA HTTP/1.1", upstream: "http://127.0.0.1:3000/socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA", host: "example.com"

iOS 错误日志:

LOG SocketIOClient: Handling event: error with data: ["Got unknown error from server <html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.10.3 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n"]

如果您需要更多信息,请告诉我!

最佳答案

我自己解决了这个问题。这真是一个愚蠢的问题。我在 Node.js 服务器文件夹中创建了一个名为 access.log 的文件,并告诉 morgan 记录器写入该文件。我忘记的是,每当服务器文件夹内的代码发生更改时,我都会使用 PM2 重新启动服务器。所以每次我发出http请求并且套接字断开连接时PM2都会重新启动服务器。

关于node.js - Socket.io 根据 http 请求断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46448515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com