11

我使用 Django Channels 创建了一个 Web 应用程序,在尝试设置 Supervisor 系统时遇到了问题。

首先,应用程序在本地运行良好。

远程(我使用带有 Ubuntu Server 18.04 LTS 的 AWS EC2 实例),当使用命令运行时daphne -b 0.0.0.0 -p 8000 mysite.asgi:application它也运行良好。

但是,我不能让它与主管一起工作。我遵循官方 Django Channels 文档(https://channels.readthedocs.io/en/latest/deploying.html)的说明,因此我有:

  • nginx配置文件:

    upstream channels-backend {
        server localhost:8000;
    }
    
    server {
        server_name www.example.com;
    
        keepalive_timeout 5;
        client_max_body_size 1m;
    
        access_log /home/ubuntu/django_app/logs/nginx-access.log;
        error_log /home/ubuntu/django_app/logs/nginx-error.log;
    
        location /static/ {
            alias /home/ubuntu/django_app/mysite/staticfiles/;
        }
    
        location / {
            try_files $uri @proxy_to_app;
        }
    
        location @proxy_to_app {
            proxy_pass http://channels-backend;
    
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    
            proxy_redirect off;
            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_set_header X-Forwarded-Host $server_name;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
    
    server {
        listen 80;
        server_name www.example.com;
    
        if ($host = www.example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
        return 404; # managed by Certbot
    }
    
  • 主管配置文件:

    [fcgi-program:asgi]
    
    socket=tcp://localhost:8000
    
    directory=/home/ubuntu/django_app/mysite
    
    command=/home/ubuntu/django_app/venv/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application
    
    numprocs=4
    
    process_name=asgi%(process_num)d
    
    autostart=true
    autorestart=true
    
    stdout_logfile=/home/ubuntu/django_app/logs/supervisor_log.log
    redirect_stderr=true
    

这样设置时,网页不工作(504 Gateway Time-out)。在主管日志文件中,我看到:

2018-11-14 14:48:21,511 INFO     Starting server at fd:fileno=0, unix:/run/daphne/daphne0.sock
2018-11-14 14:48:21,516 INFO     HTTP/2 support enabled
2018-11-14 14:48:21,517 INFO     Configuring endpoint fd:fileno=0
2018-11-14 14:48:22,015 INFO     Listening on TCP address 127.0.0.1:8000
2018-11-14 14:48:22,025 INFO     Configuring endpoint unix:/run/daphne/daphne0.sock
2018-11-14 14:48:22,026 CRITICAL Listen failure: [Errno 2] No such file or directory: '1416' -> b'/run/daphne/daphne0.sock.lock'
2018-11-14 14:48:22,091 INFO     Starting server at fd:fileno=0, unix:/run/daphne/daphne2.sock
2018-11-14 14:48:22,096 INFO     HTTP/2 support enabled
2018-11-14 14:48:22,097 INFO     Configuring endpoint fd:fileno=0
2018-11-14 14:48:22,135 INFO     Starting server at fd:fileno=0, unix:/run/daphne/daphne3.sock
2018-11-14 14:48:22,152 INFO     HTTP/2 support enabled
2018-11-14 14:48:22,153 INFO     Configuring endpoint fd:fileno=0
2018-11-14 14:48:22,237 INFO     Listening on TCP address 127.0.0.1:8000
2018-11-14 14:48:22,241 INFO     Listening on TCP address 127.0.0.1:8000
2018-11-14 14:48:22,242 INFO     Configuring endpoint unix:/run/daphne/daphne3.sock
2018-11-14 14:48:22,242 CRITICAL Listen failure: [Errno 2] No such file or directory: '1419' -> b'/run/daphne/daphne3.sock.lock'
2018-11-14 14:48:22,252 INFO     Configuring endpoint unix:/run/daphne/daphne2.sock
2018-11-14 14:48:22,252 CRITICAL Listen failure: [Errno 2] No such file or directory: '1420' -> b'/run/daphne/daphne2.sock.lock'

等等

请注意,在 Supervisor 命令中,Daphne 进程以另一种方式调用(使用其他参数集)而不是我之前运行它 - 不是地址和端口的参数,而是套接字和文件描述符的参数(关于这些我没有知道很多)。我怀疑这是遇到错误的原因。

任何帮助或建议将不胜感激。

相关软件包版本:

channels==2.1.2
channels-redis==2.2.1
daphne==2.2.1
Django==2.1.2

编辑:

当我为套接字文件(在主管配置文件中的 Daphne 命令中存在)创建空文件时,即。/run/daphne/daphne0.sock, /run/daphne/daphne1.sock, 等等,然后日志文件说明以下内容:

2018-11-15 10:24:38,289 INFO     Starting server at fd:fileno=0, unix:/run/daphne/daphne0.sock
2018-11-15 10:24:38,290 INFO     HTTP/2 support enabled
2018-11-15 10:24:38,280 INFO     Configuring endpoint fd:fileno=0
2018-11-15 10:24:38,458 INFO     Listening on TCP address 127.0.0.1:8000
2018-11-15 10:24:38,475 INFO     Configuring endpoint unix:/run/daphne/daphne0.sock
2018-11-15 10:24:38,476 CRITICAL Listen failure: Couldn't listen on any:b'/run/daphne/daphne0.sock': [Errno 98] Address already in use.

问题:这些文件不应该是空的吗?他们应该包括什么?

4

2 回答 2

2

Fabio 的回答是替换端点参数的文件描述符参数,为这个问题提供了一个快速的解决方法(这似乎是 Daphne 代码中的一个错误)。

但是,Daphne 存储库中的修复很快被提交,因此原始指令运行良好。

作为旁注(对于我在原始问题中写的仍然遇到严重监听失败的人),请确保套接字文件的物理位置(/run/daphne/在我的情况下)是可访问的 - 我花了太多时间只是为了发现在目录中创建daphne文件夹/run可以完成这项工作(即使我使用 运行所有内容sudo)...为了预防措施,可以考虑将套接字文件重定向到另一个文件夹,例如/tmp,它确实允许在未经许可的情况下创建目录sudo

于 2018-11-28T16:58:59.670 回答
2

在主管 ASGI 配置文件中,在以下行中

command=/home/ubuntu/django_app/venv/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application

替换--fd 0--endpoint fd:fileno=0.

问题:https ://github.com/django/daphne/issues/234

于 2018-11-22T22:19:28.650 回答