'Django server & Gunicorn - 502 Bad Gateway
I have running two Django servers in my raspberry pi and serve them with Gunicorn and Nginx.
One of them is running fine, but the other one is giving me a 502 Bad Gateway issue. I would appreciate if someone could help me to debug this issue.
Here are the logs when I run sudo journalctl -u portfolio:
pi@raspberrypi:/etc/systemd/system $ sudo journalctl -u portfolio
-- Logs begin at Mon 2022-03-14 11:41:15 GMT, end at Mon 2022-03-14 12:32:28 GMT. --
Mar 14 12:07:10 raspberrypi systemd[1]: Started gunicorn daemon.
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4623] [INFO] Starting gunicorn 20.1.0
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4623] [INFO] Listening at: unix:/run/portfolio.sock (4623)
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4623] [INFO] Using worker: sync
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4626] [INFO] Booting worker with pid: 4626
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4630] [INFO] Booting worker with pid: 4630
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4631] [INFO] Booting worker with pid: 4631
Mar 14 12:07:11 raspberrypi gunicorn[4623]: [2022-03-14 12:07:11 +0000] [4626] [ERROR] Exception in worker process
Mar 14 12:07:11 raspberrypi gunicorn[4623]: Traceback (most recent call last):
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
Mar 14 12:07:11 raspberrypi gunicorn[4623]: worker.init_process()
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/workers/base.py", line 134, in init_process
Mar 14 12:07:11 raspberrypi gunicorn[4623]: self.load_wsgi()
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
Mar 14 12:07:11 raspberrypi gunicorn[4623]: self.wsgi = self.app.wsgi()
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Mar 14 12:07:11 raspberrypi gunicorn[4623]: self.callable = self.load()
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Mar 14 12:07:11 raspberrypi gunicorn[4623]: return self.load_wsgiapp()
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Mar 14 12:07:11 raspberrypi gunicorn[4623]: return util.import_app(self.app_uri)
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/home/pi/portfolio_v2/backend/myvenv/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
Mar 14 12:07:11 raspberrypi gunicorn[4623]: mod = importlib.import_module(module)
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
Mar 14 12:07:11 raspberrypi gunicorn[4623]: return _bootstrap._gcd_import(name[level:], package, level)
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
Mar 14 12:07:11 raspberrypi gunicorn[4623]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
If I check sudo systemctl status portfolio everything is fine.
Here is the way I configure portfolio.socket and portfolio.service:
portfolio.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/portfolio.sock
[Install]
WantedBy=sockets.target
portfolio.service
[Unit]
Description=gunicorn daemon
Requires=portfolio.socket
After=network.target
[Service]
User=pi
Group=pi
WorkingDirectory=/home/pi/portfolio_v2/backend
EnvironmentFile=/home/pi/portfolio_v2/backend/.env
ExecStart=/home/pi/portfolio_v2/backend/myvenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/portfolio.sock \
portfolio_backend.wsgi:application
[Install]
WantedBy=multi-user.target
Solution 1:[1]
Solved.
It was a typo in my file inside /etc/nginx/sites-available
here:
location / {
include proxy_params;
proxy_pass http://unix:/run/portfolio.sock;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mário Prada |
