I'm trying to install django_channels. I pass the tutorial, and everything works except of essential - websocket doesn't work when I try it by external browser.
Daphne is working, checked with wscat --connect ws://localhost:8000/ws/chat/room/
so local works.
The only difference between tutorial and my is in asgi.py:
tutorial asgi.py:
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chat.routing
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
my asgi.py:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings')
import django
django.setup()
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing
from channels.auth import AuthMiddlewareStack
application = ProtocolTypeRouter({
"http" : get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
I have to move from channels.auth import AuthMiddleWareStack
lower and have to add import django; django.setup()
because of errors - (NO INSTALLED_APPS and no Apps).
I know that nginx got connection because /var/log/asgi.log reacts:
django.template.base.VariableDoesNotExist: Failed lookup for key [name] in <URLResolver <module 'chat.urls' from './chat/urls.py'> (None:None) 'chat/'>
2021-01-27 00:44:20,491 WARNING Not Found: /ws/chat/room/
nginx/sites-enabled/mysite.conf
#
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# the upstream component nginx needs to connect to
upstream django-mysite {
server unix:///home/mysite/mysite.sock; # for a file socket
}
upstream websocket {
server localhost:8000;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name mysite.example.com
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media/ {
alias /home/mysite/media/; # your Django project's media files - amend as required
}
location /static/ {
alias /home/common/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django-mysite;
include uwsgi_params; # the uwsgi_params file you installed
try_files $uri @daphne;
}
location @daphne {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
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;
}
}
External browser console shows:
WebSocket connection to 'ws://mysite.example.com/ws/chat/room/' failed: Error during WebSocket handshake: Unexpected response code: 404
question from:
https://stackoverflow.com/questions/65911451/nginx-doesnt-proxy-websocket 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…