使用Daphne和Nginx部署Django Channels出现问题

5
当我尝试打开一个网站时,遇到了502错误。我使用了官方网站 链接 上的指示。
已在/etc/supervisor/conf.d/目录下添加了新文件lifeline.conf。 lifeline.conf
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000

# Directory where your site's project files are located
directory=/home/ubuntu/lifeline/lifeline-backend

# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-head$

# Number of processes to startup, roughly the number of CPUs you have
numprocs=4

# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d

# Automatically start and recover processes
autostart=true
autorestart=true

# Choose where you want your log to go
stdout_logfile=/home/ubuntu/asgi.log
redirect_stderr=true

设置 nginx 配置

upstream channels-backend {
    server localhost:8000;
}

server {
    listen 80;
    server_name staging.mysite.com www.staging.mysite.com;
    client_max_body_size 30M;
    location = /favicon.ico { access_log off; log_not_found off; }

    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;
    }
}

我检查了asgi日志文件,发现其中有一个错误。

daphne: error: the following arguments are required: application

我猜测是 lifeline.conf 中出现了错误。
1个回答

2
我假设您没有将asgi应用程序传递给daphne,因为您在问题中粘贴的配置缺少一行。您必须正确传递它。假设您有带有asgi.py模块的conf包,其中包含asgi应用程序实例,则必须执行以下操作:Original Answer。
command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock conf.asgi:application

conf.asgi:application should be at the end.


感谢您的建议。更新后,我遇到了新的错误:2019-08-09 11:36:23,711 INFO Starting server at unix:/run/daphne/daphne0.sock 2019-08-09 11:36:23,714 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras) 2019-08-09 11:36:23,714 INFO Configuring endpoint unix:/run/daphne/daphne0.sock 2019-08-09 11:36:23,720 CRITICAL Listen failure: [Errno 2] No such file or directory: '2843' -> b'/run/daphne/daphne0.sock.lock' - Jekson
此外,我在/run/目录下找不到daphne文件夹。 - Jekson
4
我在/run/中创建了“daphne”文件夹,然后一切都正常工作了。 - Jekson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接