Nginx – Fixing 111: Connection Refused While Connecting to Upstream

nginxsupervisord

I am a beginner on server side processes and the other questions that address this topic are running different stacks than I am so it's been quite hard to find useable answers.

It's very possible I am making a silly error with the IP addresses in the server block but again, as I am a beginner, it is not obvious.

I'm running a Django 2.1 application on Digital Ocean using Nginx and supervisor. My application uses Django channels which uses daphne hence I am using supervisor (asgi instead of uwsgi).

I've also registered with Let's Encrypt to get my SSL certificate.

Supervisor and Nginx are running and I see a

502 Bad Gateway when I type in my domain name.

The nginx log shows

2019/02/26 15:51:40 [error] 20187#20187: *8 connect() failed (111: Connection refused) while connecting to upstream, client: 86.xxx.xxx.14, server: myapp.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "www.myapp.com"

sudo /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
shows

2019-02-26 15:52:39,429 CRIT Supervisor running as root (no user in config file)
2019-02-26 15:52:39,430 WARN Included extra file "/etc/supervisor/conf.d/appname_asgi.conf" during parsing
2019-02-26 15:52:39,431 INFO Creating socket tcp://127.0.0.1:8000
2019-02-26 15:52:39,431 INFO Closing socket tcp://127.0.0.1:8000
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.

Which obviously says another program is using port 8000.

However sudo netstat -nlp | grep 8000 returns nothing.

configuration file /etc/nginx/sites-enabled/myapp:

upstream myapp {
    server 127.0.0.1:8000;
}

server {
    server_name myapp.com www.myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        try_files $uri @proxy_to_app;
    }

    location /static/ {
        root /home/me/myapp/src/myapp;
    }

    location /media/  {
        root /home/me/myapp/src/myapp;
        include /etc/nginx/mime.types;
    }

    location @proxy_to_app {
        proxy_pass http://myapp;

        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/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.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 {
    if ($host = www.myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name myapp.com www.myapp.com;
    return 404; # managed by Certbot

/etc/supervisor/conf.d/myapp_asgi.conf

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

# Directory where your site's project files are located
directory=/home/me/myapp/src/myapp

# 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 /home/me/daphne/run/daphne%(process_num)d.sock --f$

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

sudo netstat -peanut

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      112        2954261     11650/redis-server 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          3495386     19401/nginx -g daem
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          15750       1564/sshd       
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          3499269     19401/nginx -g daem
tcp        0    356 xxx.xx.xxx.76:22        xx.xx.xxx.14:52521     ESTABLISHED 0          3417368     13924/1         
tcp        0      0 xxx.xx.xxx.76:22        xx.xx.xxx.14:52528     ESTABLISHED 0          3418136     14131/2         
tcp6       0      0 :::6379                 :::*                    LISTEN      1000       2954691     11712/redis-server 
tcp6       0      0 :::80                   :::*                    LISTEN      0          3495387     19401/nginx -g daem
tcp6       0      0 :::22                   :::*                    LISTEN      0          15761       1564/sshd     

I would appreciate if someone could look over this as, like I said, I'm sure the problem is obvious to someone with a higher skill level in this area.

Many thanks.

Best Answer

Solved it by changing to

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

so it was able to execute the daphne command