Question

Django 502 Bad Gateway

I keep getting a 502 Bad Gateway each time I try to access my web app. It was accessible prior but suddenly started showing a 502 Bad Gateway. My nginx configuration is

server {
     listen 80;
     server_name IP_ADDRESS;

     location = /favicon.ico { access_log off; log_not_found off; }
     location /static/ {
         root /home/user/pyapps/test_project;
     }
     
     location /media/ {
         root /home/user/pyapps/test_project;    
     }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Gunicorn.service

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

[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/pyapps/test_project
ExecStart=/home/user/pyapps/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          test.wsgi:application

[Install]
WantedBy=multi-user.target

sudo systemctl status gunicorn.socket gives:

● gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
     Active: failed (Result: service-start-limit-hit) since Thu 2023-03-30 06:50:20 UTC; 18min ago
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)

Mar 30 06:46:29 ... systemd[1]: Listening on gunicorn socket.
Mar 30 06:50:20 ... systemd[1]: gunicorn.socket

sudo systemctl status gunicorn.service gives:

● gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service_; disabled; vendor preset: enabled)_
     Active: failed (Result: exit-code) since Thu 2023-03-30 06:50:20 UTC_; 21min ago_
TriggeredBy: ● gunicorn.socket
    Process: 46876 ExecStart=/home/.../pyapps/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock ccpfew.wsgi:application (code=exited, status=203/EXEC)
   Main PID: 46876 (code=exited, status=203/EXEC)

Mar 30 06:50:20 ... systemd[1]: Started gunicorn daemon.
Mar 30 06:50:20 ... systemd[46876]: gunicorn.service: Failed to execute command: No such file or directory
Mar 30 06:50:20 ... systemd[46876]: gunicorn.service: Failed at step EXEC spawning /home/.../pyapps/venv/bin/gunicorn: No such file or directory
Mar 30 06:50:20 ... systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Mar 30 06:50:20 ... systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Mar 30 06:50:20 ... systemd[1]: gunicorn.service: Start request repeated too quickly.
Mar 30 06:50:20 ... systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Mar 30 06:50:20 ... systemd[1]: Failed to start gunicorn daemon.

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.

Bobby Iliev
Site Moderator
Site Moderator badge
April 1, 2023
Accepted Answer

Hi there,

As far as I can see, it looks like that the gunicorn executable cannot be found, as indicated by the error message:

gunicorn.service: Failed at step EXEC spawning /home/.../pyapps/venv/bin/gunicorn: No such file or directory

Can you confirm if you’ve followed the Creating a Python Virtual Environment for your Project step from the Django setup tutorial?

Besides that, the Nginx configuration file and the service file look correct.

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Featured on Community

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