Django Channels Nginx production

后端 未结 3 1656
借酒劲吻你
借酒劲吻你 2020-12-29 08:05

I have a django project and recently added channels to use websockets. This seems to all work fine, but the problem I have is to get the production ready.

My setup i

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 08:31

    This question is actually addressed in the latest Django Channels docs:

    It is good practice to use a common path prefix like /ws/ to distinguish WebSocket connections from ordinary HTTP connections because it will make deploying Channels to a production environment in certain configurations easier.

    In particular for large sites it will be possible to configure a production-grade HTTP server like nginx to route requests based on path to either (1) a production-grade WSGI server like Gunicorn+Django for ordinary HTTP requests or (2) a production-grade ASGI server like Daphne+Channels for WebSocket requests.

    Note that for smaller sites you can use a simpler deployment strategy where Daphne serves all requests - HTTP and WebSocket - rather than having a separate WSGI server. In this deployment configuration no common path prefix like is /ws/ is necessary.

    In practice, your NGINX configuration would then look something like (shortened to only include relevant bits):

    upstream daphne_server {
      server unix:/var/www/html/env/run/daphne.sock fail_timeout=0;
    }
    
    upstream gunicorn_server {
      server unix:/var/www/html/env/run/gunicorn.sock fail_timeout=0;
    }
    
    server { 
      listen   80; 
      server_name _;
    
      location /ws/ {
        proxy_pass http://daphne_server;
      }
    
      location / {
        proxy_pass http://gunicorn_server;
      }
    }
    

    (Above it is assumed that you are binding the Gunicorn and Daphne servers to Unix socket files.)

提交回复
热议问题