7

我有一个Django应用程序位于具有Nginxuwsgi配置的服务器中。现在我也必须与之合作Django Channels。在本地计算机上一切都很好,但我读过很多关于 Django Channels 与 uwsgi 不兼容的信息。

我尝试了很多次和许多不同的方式来配置代理nginx.confDaphne但对我没有任何作用。我有这个问题好几个月了,我找不到任何可以帮助我的东西。

设置.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
        )
    )
})

网络套接字网址

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 服务器

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).

解决方案

我已将asgi.pyrouting.py文件合并到 中,asgi.py并添加了一些信息:


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
        )
    )
})

重要的是要尊重进口的顺序

最后我nginx.conf的是:

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/;
    }
}

然后我执行: (venv) uwsgi --ini config/uwsgi.ini (venv) daphne -b 0.0.0.0 -p 8001 myproject.asgi:application

您应该创建一个文件以在 shell 关闭时daphne.service触发以继续执行。daphne

我希望这些信息可以帮助某人。

4

0 回答 0