Django Channels Nginx production

后端 未结 3 1655
借酒劲吻你
借酒劲吻你 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:34

    I have created an example how to mix Django Channels and Django Rest Framework. I set nginx routing that:

    • websockets connections are going to daphne server
    • HTTP connections (REST API) are going to gunicorn server

    Here is my nginx configuration file:

    upstream app {
        server wsgiserver:8000;
    }
    
    upstream ws_server {
        server asgiserver:9000;
    }
    
    
    server {
        listen 8000 default_server;
        listen [::]:8000;
    
        client_max_body_size 20M;
    
        location / {
            try_files $uri @proxy_to_app;
        }
    
        location /tasks {
            try_files $uri @proxy_to_ws;
        }
    
        location @proxy_to_ws {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;
    
            proxy_pass   http://ws_server;
        }
    
        location @proxy_to_app {
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Url-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
    
            proxy_pass   http://app;
        }
    
    }
    

提交回复
热议问题