I have a Django
application located in a server with Nginx
and uwsgi
configuration. And now I have to work with Django Channels
too. In a local computer all it's fine but I have read a lot about that Django Channels is not compatible with uwsgi.
I tried to configure many times and many different ways a proxy in nginx.conf
with Daphne
but nothing works for me. I have this problem for several months and I can't find anything that can help me.
settings.py
# WSGI
WSGI_APPLICATION = 'config.wsgi.application'
# Channels
ASGI_APPLICATION = 'config.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
config.routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myproject.websocket.routing
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
myproject.websocket.routing.websocket_urlpatterns
)
)
})
websocket url
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/socket/$', consumers.WebSocketConsumer),
]
asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_asgi_application()
nginx.conf
upstream myproject {
server unix:///tmp/myproject.sock;
}
upstream channels {
server localhost:9001;
}
server {
listen 80;
server_name myprojectdomain;
client_max_body_size 100M;
if ($host !~* ^(myprojectdomain)$ ) {
return 444;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_params myproject;
}
location /static/ {
alias /home/myproject/httpdocs/static/;
}
location /media/ {
alias /home/myproject/httpdocs/myproject/media/;
}
location /ws/ {
proxy_pass channels;
}
}
uwsgi.ini
[uwsgi]
virtualenv = /home/myproject/httpdocs/venv
pythonpath = /home/myproject/httpdocs
socket = /tmp/myproject.sock
chmod-socket = 664
module=config.wsgi:application
master = true
daemonize = /var/log/uwsgi/myproject.log
pidfile = /tmp/myproject.pid
(venv) uwsgi --ini config/uwsgi.ini
(venv) daphne -p 9001 myproject.asgi:application
2021-02-04 21:04:13,666 INFO Starting server at tcp:port=9001:interface=127.0.0.1
2021-02-04 21:04:13,667 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2021-02-04 21:04:13,667 INFO Configuring endpoint tcp:port=9001:interface=127.0.0.1
2021-02-04 21:04:13,668 INFO Listening on TCP address 127.0.0.1:9001
redis-server
root@... redis-server
199857:C 05 Feb 2021 07:38:22.344 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
199857:C 05 Feb 2021 07:38:22.344 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=199857, just started
199857:C 05 Feb 2021 07:38:22.344 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
199857:M 05 Feb 2021 07:38:22.344 * Increased maximum number of open files to 10032 (it was originally set to 1024).
Edited answer out of OP's question:
I have merged the asgi.py
and routing.py
files into the asgi.py
and I add it some information:
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myproject.websocket.routing
application = ProtocolTypeRouter({
"http": asgi_app,
'websocket': AuthMiddlewareStack(
URLRouter(
myproject.websocket.routing.websocket_urlpatterns
)
)
})
Is important to respect the order of the imports.
Finally my nginx.conf
is:
myproject {
server unix:///tmp/myproject.sock;
}
server {
listen 80;
server_name mydomain;
client_max_body_size 100M;
if ($host !~* ^(mydomain)$ ) {
return 444;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass myproject;
}
location /ws/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
alias /home/myproject/httpdocs/static/;
}
location /media/ {
alias /home/myproject/httpdocs/liszt/media/;
}
}
Then I execute:
(venv) uwsgi --ini config/uwsgi.ini
(venv) daphne -b 0.0.0.0 -p 8001 myproject.asgi:application
You should be create a daphne.service
file to trigger to keep executing daphne
when shell is closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With