使用 nginx 部署频道

deploying channels with nginx

安装后我已经按照 digital ocean. Then I blindly followed the section "Example Setup" in the channels document 中的教程使用 nginx 部署了 django。

我的困惑是:

  1. 设置supervisor的配置文件时,说要设置目录为

directory=/my/app/path

我应该写下manage.py所在的路径还是settings.py所在的路径?

  1. 当我在更改 nginx 配置文件后重新加载 nginx 时,我得到一个错误提示

host not found in upstream "channels-backend" in /etc/nginx/sites-enabled/mysite:18 nginx: configuration file /etc/nginx/nginx.conf test failed

我确实用我的网站名称替换了“mysite”。我之前有另一个错误说

no live upstreams while connecting to upstream

但无法重现这种情况。

我是频道的新手,所以有关上游的任何其他信息都会有所帮助。如果我需要提供更多信息,请告诉我。

编辑:

这是 nginx.conf 文件。我更改了 <> 中的一些敏感数据。

upstream channels-backend {
    server localhost:8000;
}

server {
    listen 80;
    server_name <domain name> <ip address>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root <root to static>;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
        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;
    }
}

这通过了 nginx -t。 error.log

中的错误消息
connect() failed (111: Connection refused) while connecting to upstream, client: <some ip>, server: <domain name>, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "<domain name>"

问题实际上在 supervisor 配置文件中。

[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000

# Directory where your site's project files are located
directory=/my/app/path

# 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=daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application

# 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=/your/log/asgi.log
redirect_stderr=true

为了检查主管是否 运行 正确,我 运行

sudo supervisorctl status

这给了我致命的状态。问题是我目前使用的是虚拟环境,daphne 只安装在虚拟环境中。因此,您的命令应该类似于

command= /my/project/virtualenv/path/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application