Deploying Django Channels with nginx
I am trying to deploy the Django application in AWS ubuntu os with Django channels, Using Nginx.
I had configured the Django server in Nginx.
But I don't know how to configure channels or Redis-server in Nginx.
My nginx config is as follows:
server {
listen 80;
server_name 52.77.215.218;
location / {
include proxy_params;
proxy_pass http://localhost:8000/
}
}
My settings.py:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
My requirements.txt:
aioredis==1.3.1
asgiref==3.5.2
async-timeout==4.0.2
attrs==22.1.0
autobahn==22.7.1
Automat==20.2.0
certifi==2022.6.15
cffi==1.15.1
channels==3.0.5
channels-redis==2.4.2
charset-normalizer==2.1.1
constantly==15.1.0
coreapi==2.3.3
coreschema==0.0.4
cryptography==37.0.4
daphne==3.0.2
defusedxml==0.7.1
Django==4.1
django-cors-headers==3.13.0
django-templated-mail==1.1.1
djangorestframework==3.13.1
djangorestframework-simplejwt==4.8.0
djoser==2.1.0
gunicorn==20.1.0
hiredis==2.0.0
hyperlink==21.0.0
idna==3.3
incremental==21.3.0
itypes==1.2.0
Jinja2==3.1.2
MarkupSafe==2.1.1
msgpack==0.6.2
mysql==0.0.3
mysqlclient==2.1.1
oauthlib==3.2.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
PyJWT==2.4.0
pyOpenSSL==22.0.0
python3-openid==3.2.0
pytz==2022.2.1
requests==2.28.1
requests-oauthlib==1.3.1
service-identity==21.1.0
six==1.16.0
social-auth-app-django==4.0.0
social-auth-core==4.3.0
sqlparse==0.4.2
Twisted==22.4.0
twisted-iocpsupport==1.0.2
txaio==22.2.1
typing_extensions==4.3.0
tzdata==2022.2
uritemplate==4.1.1
urllib3==1.26.12
zope.interface==5.4.0
When I run server with python3 manage.py runserver 0.0.0.0:8000
Server running good and also connecting with redis-server but when
I run server with gunicorn app.wsgi -b 0.0.0.0:800
then failed to
connect with webbsocket.
I also tried Hostinger VPS as well, but Same Issue.
You will need to download Daphne.
Daphne is a high-performance websocket server for Django channels.
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/daphne/
https://github.com/django/daphne
https://channels.readthedocs.io/en/stable/deploying.html
How you can run daphne:
daphne -b 0.0.0.0 -p 8070 django_project.asgi:application
Here is my Nginx conf for channels:
upstream django {
server 127.0.0.1:8080;
}
upstream websockets{
server 127.0.0.1:8070;
}
server {
...
...
location /ws {
proxy_pass http://websockets;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
...
}
location / {
proxy_pass http://django;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
...
}
}