502 bad gateway nginx with Django App

I just create a new data table in Django app and then I sudo reboot it. I got 502 Bad gat way errors.

sudo tail -30 /var/log/nginx/error.log

Here is the error code I find out:

2021/09/23 20:17:51 [crit] 983#983: *11 connect() to unix:/run/gunicorn/socket failed (2: No such file or directory) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn/socket:/", host: ""

Here is what I tried:

  • This says my configuration is ok and successfully
sudo nginx -t
  • I check if the nginx is running
systemctl status nginx


● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-09-23 19:57:39 UTC; 22min ago
     Docs: man:nginx(8)
  Process: 977 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 935 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 982 (nginx)
    Tasks: 3 (limit: 2360)
   CGroup: /system.slice/nginx.service
           ├─982 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─983 nginx: worker process
           └─984 nginx: worker process
  • Tried these commands:
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl restart nginx
  • Then this one:
which gunicorn

Output: /usr/bin/gunicorn

Then I go to /etc/systemd/system/gunicorn.service and find out the following text:

Description=gunicorn daemon

          --access-logfile - \
          --pid /run/gunicorn/pid   \
          --workers 3 \
          --env DJANGO_SETTINGS_MODULE=springgem.settings \
          --bind unix:/run/gunicorn/socket \
          --pythonpath '/home/shane/springgem,/home/shane/springgem/coronavirus_tracker' \
          --chdir '/home/shane/springgem' \
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID


I realize the ExecStart path is different so I change to the path that generate from which gunicorn. And run following commands. But it still not working.

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

Thanks for any help!


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

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.


Nginx seems to be working as expected, this looks like a problem with the Gunicorn service as you are getting the following error:

connect() to unix:/run/gunicorn.sock failed (2: No such file or directory)

This indicates that Nginx was unable to find the gunicorn.sock file at the given location.

You should compare the proxy_pass location defined within /etc/nginx/sites-available/your_project file to the actual location of the gunicorn.sock file generated by the gunicorn.socket systemd unit.

If you cannot find a gunicorn.sock file within the /run directory, it generally means that the systemd socket file was unable to create it. For this I would recommend going through the section on checking for the Gunicorn socket file to step through the troubleshooting steps for Gunicorn from this tutorial here:

Regards, Bobby