Search code examples
djangonginxdjango-channels

deploying channels with nginx


I have deployed django with nginx following the tutorials in digital ocean. Then I blindly followed the section "Example Setup" in the channels document after installation.

My confusions are:

  1. When setting up the configuration file for supervisor, it says to set the directory as

directory=/my/app/path

Should I write down the path where the manage.py is or the path where the settings.py is?

  1. When I reload nginx after changing nginx configuration file, I get an error saying that

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

I did replace "mysite" by the name of my website. I had another error earlier saying that

no live upstreams while connecting to upstream

but could not recreate the situation.

I am new to using the channels, so any additional information on upstream would be helpful. Please let me know if I need to provide more information.

Edit:

Here is the nginx.conf file. I changed some sensitive data inside the <>.

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

This passes nginx -t. The error message I have in the 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>"

Solution

  • The problem actually in supervisor configuration file.

    [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
    

    To check if supervisor was running correctly, I ran

    sudo supervisorctl status
    

    This gave me a FATAL status. The problem was that I am currently using a virtual environment, and daphne was only installed inside the virtual environment. Therefore your command should be something like

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