14

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

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

我设法安装和设置 ASGI,然后用 daphne 运行服务器,但只有一个工作人员(据我所知,这是 ASGI 的限制),但工作人员的负载太高了。

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

4

1 回答 1

22

可以将 WSGI 与 ASGI 一起运行,这是 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;
    }
}

/ws/正确使用,您需要像这样输入您的 URL:

ws://localhost/ws/your_path

然后nginx就可以升级连接了。

于 2018-03-14T01:35:57.870 回答