nginx – 502 Error and *2 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream


I’ve encountered this issue on a couple of servers after routine maintenance, and I don’t know what caused them to fail. I am running Nginx, Gunicorn, and Django.

The commands I ran:

> alias please="sudo"
> please apt update
> please apt upgrade
> more ~/.config/fish/functions/update-pip.fish 
function update-pip --description "Update pip, packages, and requirements.txt"
  if ( -e requirements.txt )
    pip install -r requirements.txt -U pip

    if ( $status -ne 0 )
      return 1
    end

    pip freeze | sed s/==/>=/ > requirements.txt
  end

  return 0
end
> upgrade-pip
> please reboot

After that, I saw a 502 error (Bad Gateway).

Nginx config:

server {
    server_name example.com www.example.com;

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

    location / { 
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }   

    location = /favicon.ico {
        root /home/matt/Project-Dir/staticfiles/home/img;
        #access_log off;
        #log_not_found off;
    }   
    
    location /static {
        alias /home/matt/Project-Dir/staticfiles;
    }   

    location /media {
        root /home/matt/Project-Dir/media;
    }   

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot 
    ssl_certificate_key /etc/letsencrypt/live/example.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.example.com) {
        return 301 https://example.com$request_uri;
    } # managed by Certbot

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

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

Nginx error log:

2020/09/25 18:33:37 (crit) 417#417: *2 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: <MY_WAN_IP>, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "example.com"
2020/09/25 18:33:37 (error) 417#417: *2 open() "/home/matt/Project-Dir/staticfiles/home/img/favicon.ico" failed (2: No such file or directory), client: <MY_WAN_IP>, server: example.com, request: "GET /favicon.ico HTTP/1.1", host: "example.com", referrer: "https://example.com/"
2020/09/25 18:34:11 (crit) 417#417: *2 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: <MY_WAN_IP>, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "example.com"

How do I resolve this?