可以仅使用Nginx和Daphne来提供Django Channels应用程序吗?

时间:2019-03-07 09:15:38

标签: django nginx django-channels daphne

我假设我可以只使用Django Channels(ASGI)和Daphne作为我的Django应用程序的代理来运行Nginx应用程序。

该应用程序将与Daphne上的127.0.0.1:8001一起运行

但是,我遇到了403 Forbidden错误。

2019/03/06 17:45:40 [error] *1 directory index of "/home/user1/app/src/app/" is forbidden

当我发表有关此事时,另一个用户提到了

  

没有指令将http请求传递给您的django应用   nginx配置

并建议研究fastcgi_passuwsgi_passGunicorn

很明显,Django Channels在ASGI上运行,我现在正在通过该请求传递所有请求(根据请求,不传递给uWSGI,然后传递给ASGI。)

我可以仅使用NginxDaphne来提供我的Django应用程序吗? Django Channels docs似乎在想,因为他们没有提到需要Gunicorn或类似的东西。

我的nginx配置

upstream socket {
    ip_hash;
    server 127.0.0.1:8001 fail_timeout=0;
}

server {

    listen 80;
    #listen [::]:80 ipv6only=on;

    server_name your.server.com;
    access_log /etc/nginx/access.log;

    root /var/www/html/someroot;

    location / {
            #autoindex on;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri =404;

            #proxy_set_header X-Real-IP $remote_addr;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header Host $http_host;
            #proxy_set_header X-NginX-Proxy true;
            #proxy_pass http://socket;
            #proxy_redirect off;
            #proxy_http_version 1.1;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "upgrade";

            #proxy_redirect off;
            #proxy_set_header   X-Forwarded-Proto $scheme;
            #proxy_cache one;
            #proxy_cache_key sfs$request_uri$scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
    # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem; 
    # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    }
}

1 个答案:

答案 0 :(得分:1)

是的,有可能。试试这个配置:

upstream socket {
    ip_hash;
    server $DAPHNE_IP_ADDRESS$ fail_timeout=0;
}

server {
    ...

    location / {
        proxy_pass http://socket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    ...
}

其中$ DAPHNE_IP_ADDRESS $-您的daphne IP和不带模式的端口(127.0.0.1:8001)。