16

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

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.

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.

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 ?

Pyvonix
  • 757
  • 9
  • 22
  • 2
    If you use a load balancer/proxy server like nginx you can route requests depending on their URL or Upgrade headers to uWSGI or Daphne, yes. See http://channels.readthedocs.io/en/1.x/deploying.html#running-asgi-alongside-wsgi – yofee Feb 16 '18 at 13:45
  • The documentations talks about `Running ASGI alongside WSGI` (what I want) but there is not example code about how it's possible to implement this `load-balancer` with `Upgrade: WebSocket`. Is it possible to have more precision and some example ? – Pyvonix Feb 19 '18 at 21:22
  • @yofee Can you look at to this similar [question](https://stackoverflow.com/q/58778048/8353711)? – shaik moeed Dec 03 '19 at 18:40

1 Answers1

27

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

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

ws://localhost/ws/your_path

Then nginx will be able to upgrade the connection.

Kim Desrosiers
  • 744
  • 7
  • 13