Report this

What is the reason for this report?

502 bad gateway nginx with Django App

Posted on September 23, 2021

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!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.