Question

502 bad gateway nginx with Django App

Posted September 23, 2021 285 views
NginxUbuntuPythonDjango

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: 100.15.117.115, server: sg-weather.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn/socket:/", host: "sg-weather.com"

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

Output:

● 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:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
PIDFile=/run/gunicorn/pid
User=shane
Group=www-data
RuntimeDirectory=gunicorn
WorkingDirectory=/home/shane/springgem
ExecStart=/home/shane/springgem/springgem_env/bin/gunicorn\1
          --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' \
          springgem.wsgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

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!

edited by bobbyiliev

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.

×
Submit an Answer
1 answer

Hello,

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:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04

Regards,
Bobby

by Erin Glass
Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server is required.