使用Daphne和Nginx部署Django频道时遇到问题

时间:2019-08-09 10:54:40

标签: django nginx django-channels nginx-config daphne

尝试打开网站时出现502错误。我使用了官方网站link

上的说明

/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 conf

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 个答案:

答案 0 :(得分:1)

我假设您没有将asgi应用程序传递给daphne,因为您所粘贴的配置缺少行。您必须正确通过。假设您在内部包含asgi应用程序实例的package模块中有asgi.py个模块,您必须这样做

command=/home/ubuntu/Env/lifeline/bin/daphne -u /run/daphne/daphne%(process_num)d.sock conf.asgi:application

conf.asgi:application应该在结尾。