Daphne websocket 在一段时间后停止响应

问题描述 投票:0回答:1

项目规格:Django

3.2.4
、Channels
3.0.5
、Channel-redis
3.4.1
、Daphne
3.0.2

我设置了一个聊天系统,使用 websocket 进行通信。页面加载时,一切正常,但几秒钟后(约 20-30 秒),数据发送和接收停止。在刷新页面时,它再次开始工作。我不确定,这个问题是由我的服务器的消费者文件或 Daphne 引起的。下面是我的达芙妮

--access-log

enter image description here

我尝试调试consumers.py,但没有发现任何结果,但不确定如何调试daphne或服务器端问题。请指导我一些清单来跟踪问题。我希望连接至少保持 30 分钟。

为了在 nginx 上设置 daphne,我遵循了 this 教程。

django nginx websocket django-channels daphne
1个回答
0
投票

当连接在大约 30 秒后断开时,可能是因为 Nginx 正在终止它。我自己已经遇到过几次这个问题了。解决办法很简单。

请在 nginx 配置中将

proxy_read_timeout
设置为非常高的数字。 例如24小时。

你的配置将如下所示:

upstream channels-backend {server localhost:8000;}

server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir/django_project_dir;
}
location /media/ {
root /home/sammy/myprojectdir/django_project_dir;
}
location / {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_read_timeout 86400s;
}
}

考虑阅读本文:

© www.soinside.com 2019 - 2024. All rights reserved.