Django 频道 - 客户端有时会收到消息,有时不会收到消息,直到根本没有收到消息
Django channels - Client receives message sometimes and not other times till it doesn't recieve it at all
更新
我认为问题出在 python 版本上。在我的本地主机上,当我有 python 3.6 时它工作正常。在生产中,问题首先出现的地方,我有 3.8。在我将我的本地主机 python 版本更改为 3.8 后,我可以看到与生产中相同的问题。
原版POST
我在使用 Daphne + Nginx 设置 django-channels 时遇到问题。
当我重新启动服务器并在浏览器中打开页面时,客户端从 websocket 获取(项目)列表。当我刷新页面时,列表不会加载。然后在另一次刷新时加载。在另一次刷新时,它不会加载等等。最后,即使在多次刷新后它也不会加载。但是,如果我再次重新启动服务器并在浏览器中重新加载页面,列表将像以前一样加载,并且在下次刷新时不会加载,依此类推。
看开发者工具websocket好像总是连接上。
我已经尝试在 google 和 SO 上深入研究它,但似乎没有找到问题的原因。
这是我的 nginx 配置文件:
upstream backserver {
server localhost:8000;
}
server {
server_name MY_IP mydomain.com www.mydomain.com;
root /home/muser/myproject;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/myuser/myproject/static/;
}
if (-f $document_root/maintenance_ON.txt) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /site_maintenance.html break;
}
location / {
proxy_pass http://backserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
listen [::]:443 ipv6only=on ssl; ##added by me(developer)
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80 ipv6only=on;
server_name MY_IP mydomain.com www.mydomain.com;
return 404; # managed by Certbot
}
此外,我在日志中收到此错误。我认为它只在重启时出现。
ERROR Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=121.470065138 AsyncioSelectorReactor.callLater.<locals>.run() at /home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py", line 290, in run
f(*args, **kwargs)
File "/home/myuser/path_to_virtual_env/lib/python3.8/site-packages/daphne/server.py", line 275, in application_checker
exception = application_instance.exception()
asyncio.exceptions.CancelledError
请帮助我花了一整天的时间。
谢谢
问题似乎出在 Python 版本上。出现此问题时我正在使用 Python3.8。将 Python 版本更改为 3.6 后,问题似乎已解决。我没有尝试 3.7 版本。
更新
我认为问题出在 python 版本上。在我的本地主机上,当我有 python 3.6 时它工作正常。在生产中,问题首先出现的地方,我有 3.8。在我将我的本地主机 python 版本更改为 3.8 后,我可以看到与生产中相同的问题。
原版POST
我在使用 Daphne + Nginx 设置 django-channels 时遇到问题。
当我重新启动服务器并在浏览器中打开页面时,客户端从 websocket 获取(项目)列表。当我刷新页面时,列表不会加载。然后在另一次刷新时加载。在另一次刷新时,它不会加载等等。最后,即使在多次刷新后它也不会加载。但是,如果我再次重新启动服务器并在浏览器中重新加载页面,列表将像以前一样加载,并且在下次刷新时不会加载,依此类推。
看开发者工具websocket好像总是连接上。
我已经尝试在 google 和 SO 上深入研究它,但似乎没有找到问题的原因。
这是我的 nginx 配置文件:
upstream backserver {
server localhost:8000;
}
server {
server_name MY_IP mydomain.com www.mydomain.com;
root /home/muser/myproject;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/myuser/myproject/static/;
}
if (-f $document_root/maintenance_ON.txt) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
rewrite ^(.*)$ /site_maintenance.html break;
}
location / {
proxy_pass http://backserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
listen [::]:443 ipv6only=on ssl; ##added by me(developer)
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80 ipv6only=on;
server_name MY_IP mydomain.com www.mydomain.com;
return 404; # managed by Certbot
}
此外,我在日志中收到此错误。我认为它只在重启时出现。
ERROR Exception in callback AsyncioSelectorReactor.callLater.<locals>.run() at /home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287
handle: <TimerHandle when=121.470065138 AsyncioSelectorReactor.callLater.<locals>.run() at /home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py:287>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/home/myuser/path_to_virtual_env/lib/python3.8/site-packages/twisted/internet/asyncioreactor.py", line 290, in run
f(*args, **kwargs)
File "/home/myuser/path_to_virtual_env/lib/python3.8/site-packages/daphne/server.py", line 275, in application_checker
exception = application_instance.exception()
asyncio.exceptions.CancelledError
请帮助我花了一整天的时间。
谢谢
问题似乎出在 Python 版本上。出现此问题时我正在使用 Python3.8。将 Python 版本更改为 3.6 后,问题似乎已解决。我没有尝试 3.7 版本。