使用Django同时运行UWSGI和ASGI

Buk*_*uky 11 django uwsgi django-channels daphne

我目前正在运行一个带有uWSGI的Django(2.0.2)服务器,有10个工作人员

我正在尝试实现实时聊天,我看了一下Channel.该文档提到服务器需要与Daphne一起运行,而Daphne需要一个名为ASGI的UWSGI异步版本.

我管理安装和设置ASGI,然后用daphne运行服务器,但只有一个工作者(我理解的ASGI的限制),但负载对于工作者来说太高了.

是否可以使用带有10个工作程序的uWSGI运行服务器来回复HTTP/HTTPS请求并使用ASGI/Daphne进行WS/WSS(WebSocket)请求?或者也许可以运行ASGI的多个实例?

Kim*_*ers 10

可以在ASGI旁边运行WSGI,这是Nginx配置的一个例子:

server {
    listen 80; 

    server_name {{ server_name }};
    charset utf-8;


    location /static {
        alias {{ static_root }};
    }

    # this is the endpoint of the channels routing
    location /ws/ {
        proxy_pass http://localhost:8089; # daphne (ASGI) listening on port 8089
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
        proxy_pass http://localhost:8088; # gunicorn (WSGI) listening on port 8088
        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_connect_timeout 75s;
        proxy_read_timeout 300s;
        client_max_body_size 50m;
    }
}
Run Code Online (Sandbox Code Playgroud)

/ws/正确使用,您需要输入以下URL:

ws://localhost/ws/your_path
Run Code Online (Sandbox Code Playgroud)

然后nginx将能够升级连接.

  • 会话通常存储在数据库或其他共享位置中 (2认同)