问题
UPDATE
I think the issue is with python version. On my localhost it worked fine when I had python 3.6. On production, where the issue appeared first, I have 3.8. After I changed my localhost python version to 3.8 I could see the same issue as was on the production.
ORIGINAL POST
I am facing an issue with setting up django-channels with Daphne + Nginx.
When I reboot the server and open the page in the browser the client gets the list(of items) from websocket. When I refresh the page the list doesn't load. Then on another refresh it loads. On another refresh it doesn't load and so on. Finally it doesn't load at even after many refreshes. But if I reboot the server again and reload the page in the browser the list loads as before and on next refresh it doesn't and so on.
The websocket always seems to connect when looking at the developer tool.
I have tried digging into it on google and SO but doesn't seem to get to the cause of the problem.
here is my nginx configuration file:
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
}
Also, I get this error in the log. I think it only appears at reboot.
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
Please help I have spend whole day and evening on this.
Thanks
回答1:
It seems the issue was due to Python version. I was using Python3.8 when this issue appeared. After changing Python version to 3.6 the issue seems fixed. I did not try version 3.7.
来源:https://stackoverflow.com/questions/63225354/django-channels-client-receives-message-sometimes-and-not-other-times-till-it