本文介绍了与Django同时运行UWSGI和ASGI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用运行Django(2.0.2)服务器有10个工作人员

I'm currently running a Django (2.0.2) server with uWSGI having 10 workers

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

I'm trying to implement a real time chat and I took a look at Channel.The documentation mentions that the server needs to be run with Daphne, and Daphne needs an asynchronous version of UWSGI named ASGI.

我设法安装和设置ASGI,然后使用daphne运行服务器,但只有一名工人(据我所知,这是ASGI的局限性),但对工人来说负载太高了。

I manged to install and setup ASGI and then run the server with daphne but with only one worker (a limitation of ASGI as I understood) but the load it too high for the worker.

是否可以在10名工作人员的情况下使用uWSGI运行服务器以回复HTTP / HTTPS请求,并使用ASGI / Daphne进行WS / WSS(WebSocket)请求?
或者也许可以运行多个ASGI实例?

Is it possible to run the server with uWSGI with 10 workers to reply to HTTP/HTTPS requests and use ASGI/Daphne for WS/WSS (WebSocket) requests ?Or maybe it's possible to run multiples instances of ASGI ?

推荐答案

可以在WSGI和ASGI一起运行Nginx配置的示例:

It is possible to run WSGI alongside ASGI here is an example of a Nginx configuration:

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 / 正确,您将需要输入以下网址:

To use the /ws/ correctly, you will need to enter your URL like that:

ws://localhost/ws/your_path

然后nginx将能够升级连接。

Then nginx will be able to upgrade the connection.

这篇关于与Django同时运行UWSGI和ASGI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 22:47