Reputation: 43
I followed tutorial for deploying websockets for Django-channels using Daphne and Nginx. Here is what my NGINX.conf at etc/nginx/sites-enabled/ looks like
server {
listen 80;
server_name my_server_ip;
charset utf-8;
client_max_body_size 128M;
location /static {
# exact path to where your static files are located on server
# [mostly you won't need this, as you will be using some storage service for same]
location /static {
alias /mywebsite/static;
}
}
location /media {
# exact path to where your media files are located on server
# [mostly you won't need this, as you will be using some storage service for same]
alias /mywebsite/media;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/mywebsite/run/uwsgi.sock;
uwsgi_read_timeout 300s;
#path to proxy my WebSocket requests
location /ws/ {
proxy_pass http://0.0.0.0:9001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header 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;
}
access_log /mywebsite/log/dev-nginx-access.log;
error_log /mywebsite/log/dev-nginx-error.log;
}
Now my daphne.service file looks like below
[Unit]
Description=Mywebsite Daphne Service for websockets
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/mywebsite/
#edit myproject
#If your daphne is not installed globally and is inside your #environment, enter the complete path to daphne
ExecStart=/mywebsite/bin/daphne -b 0.0.0.0 -p 9001 mywebsite.asgi:application
Restart=on-failure
#KillSignal=SIGQUIT
#Type=notify
#NotifyAccess=all
[Install]
WantedBy=multi-user.target
the asgi.py file looks like below
"""
ASGI config for mywebsite project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chatapp.routing
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
dark.routing.websocket_urlpatterns
)
),
})
my routing.py file looks like below
# chatapp/routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),
]
my JavaScript code for the above configuration is ws://my_server_ip:9001/ws/chat/lobby/
the entire setup works without NGINX I mean if I just do python manage.py runserver 0.0.0.0:8080
and have code for WebSocket in my JavaScript as ws://my_server_ip:8080/ws/chat/lobby/
it works fine and smooth
apart from this the wsgi aspect of my webapp is running on uwsgi and running quite successfully
I have deployed it on Ubuntu 20.04 on AWS EC2 instance. I am getting the trouble connecting my WebSocket through NGINX.
when I check error logs it shows nothing and in the access.log it shows like
42.105.172.132 - - [19/Feb/2022:19:26:48 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"
42.105.172.132 - - [19/Feb/2022:19:26:53 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"
---------------focus on line below this is the access log giving 400 status---------------------
***42.105.172.132 - - [19/Feb/2022:19:27:00 +0000] "GET /ws/chat/lobby/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"***
------------------------------------------------------------------------------------------------
42.105.172.132 - - [19/Feb/2022:19:27:01 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"
I spend days on it to find a solution but could not able to find one. How do I solve this? I also checked the status of all service files of daphne, nginx, uwsgi, redis all are working fine and enabled to get started by systemctl.
Is there any error in my daphne.service and/or nginx.conf? I would be grateful for whatever direction I could get.
Upvotes: 0
Views: 1039
Reputation: 43
I am posting this answer so that It could be useful for everyone facing the issue I faced...The only point I needed to do was to open the 9001 port on ec2 management console that's it!! this solved the issue!
So you should open which ever post that u have in your daphne -b 0.0.0.0 -p 9001 mywebsite.asgi:application
after the -p
here it is 9001
.
Upvotes: 1