【问题标题】:Possible to serve Django Channels app only using Nginx and Daphne?可以仅使用 Nginx 和 Daphne 为 Django Channels 应用程序提供服务吗?
【发布时间】:2019-07-29 02:50:12
【问题描述】:

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

应用程序将在 127.0.0.1:8001 上使用 Daphne 运行

但是,我遇到了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;
    }
}

【问题讨论】:

    标签: django nginx django-channels daphne


    【解决方案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)。

    【讨论】:

    • 感谢您的评论,我知道我没疯!我已经尝试实施您的上述解决方案,但我仍然面临同样的问题403 Forbidden....directory index of "/path/to/app/" is forbidden您知道为什么会这样吗?
    • @Trilla 使用最近的配置更新您的答案。看起来它不能 proxy_pass 给 daphne。也许您的请求是由不同的服务器配置接收的?请添加“error_log /etc/nginx/error.log;”到你的配置并检查error.log,它可能会告诉你一些事情。
    • 我刚刚更新了我的答案。错误日志只是说"2019/03/07 11:23:48 [error] 16642#16642: *11 directory index of "/path/to/app" is forbidden, client: xx.xxx.xx.xx, server: app.com, request: "GET / HTTP/1.1", host: "www.app.com", referrer: "http://www.app.com""
    • 我必须完全取出 try_files 行才能正常工作。
    猜你喜欢
    • 2020-06-09
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2018-09-03
    相关资源
    最近更新 更多