Problem Description:
I’m encountering an issue with my Gunicorn setup while running a Django project. Specifically, I have two instances of Gunicorn running the same Django project: one using nohup and the other implemented as a systemd service. Surprisingly, the instance running with nohup works perfectly fine, while the one set up as a systemd service consistently returns a 400 error.
Background Information:
- I’m running a Django project with Gunicorn as the WSGI server.
- Each instance of Gunicorn is configured identically.
- Both instances are using the same Django project and Gunicorn configuration.
Steps Taken:
-
nohup Instance
: I’m starting thenohup
instance using the following command:<code>nohup /home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000 backend.wsgi:application &</code><code>nohup /home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000 backend.wsgi:application & </code>nohup /home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000 backend.wsgi:application &
-
It works perfectly fine!
-
Run Gunicorn as a systemd service, create or open the config file:
sudo nano /etc/systemd/system/gunicorn.service
-
Following is
gunicorn.service
<code>[Unit]Description=gunicorn daemonAfter=network.target[Service]User=userGroup=userWorkingDirectory=/home/user/project_backendExecStart=/home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000ExecReload=/bin/kill -s HUP $MAINPIDKillMode=mixedTimeoutStopSec=5[Install]WantedBy=multi-user.target</code><code>[Unit] Description=gunicorn daemon After=network.target [Service] User=user Group=user WorkingDirectory=/home/user/project_backend ExecStart=/home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000 ExecReload=/bin/kill -s HUP $MAINPID KillMode=mixed TimeoutStopSec=5 [Install] WantedBy=multi-user.target </code>[Unit] Description=gunicorn daemon After=network.target [Service] User=user Group=user WorkingDirectory=/home/user/project_backend ExecStart=/home/user/project_backend/venv/bin/gunicorn --workers 5 --bind 0.0.0.0:8000 ExecReload=/bin/kill -s HUP $MAINPID KillMode=mixed TimeoutStopSec=5 [Install] WantedBy=multi-user.target
-
and thenrun
sudo systemctl daemon-reload
to apply the config change andsudo systemctl start gunicorn
-
Attempted to send the same HTTP request to both instances:
- Request sent to nohup instance: success
- Request sent to systemd service instance: failure (returned 400 error)
-
Has anyone encountered a similar issue with Gunicorn and systemd services?
-
What further steps can I take to diagnose and resolve this inconsistency between the two instances?
-
Are there any specific configuration differences between running Gunicorn with nohup and as a systemd service that could potentially cause this behavior?
Main Jay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.