Question

connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream

I am trying to upload my django project to digitalocean droplet. Followed this article https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04 to upload it.

After completing all the steps I am getting 502 bad gateway. Also the server is not running on SSL rather it is running on http//.

I followed every steps the article provided. Somehow there is an error called “connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream”

/etc/systemd/system/gunicorn.socket -----

[Unit]
    Description=gunicorn socket
    
    [Socket]
    ListenStream=/run/gunicorn.sock
    
    [Install]
    WantedBy=sockets.target

/etc/systemd/system/gunicorn.service ----

[Unit]
    Description=gunicorn daemon
    Requires=gunicorn.socket
    After=network.target
    
    [Service]
    User=sammy
    Group=www-data
    WorkingDirectory=/home/sammy/myprojectdir
    ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
              --access-logfile - \
              --workers 3 \
              --bind unix:/run/gunicorn.sock \
              myproject.wsgi:application
    
    [Install]
    WantedBy=multi-user.target

/etc/nginx/sites-available/myproject -----

server {
        listen 80;
        server_name server_domain_or_IP;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/sammy/myprojectdir;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }

I ran this commands but getting no error

sudo nginx -t && sudo systemctl restart nginx

So I need help on this.

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
April 16, 2024
Pinned Answer

Heya all,

An update on an older topic.The error failed (111: Connection refused) while connecting to upstream” suggest an issue with the Application itself rather than the configuration files (Nginx/Gunicorn/etc.).

As such, you’ll always need to review the APP’s error logs rather than your WebServices.

The content of /etc/systemd/system/gunicorn.service and /etc/nginx/sites-available/myproject appears to be exactly what’s in the howto link you provided. Is that the problem? For example, your user probably isn’t sammy

Hi @irfan015

If you haven’t done so already. Restart both the gunicorn service and the nginx service.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel