Django 服务器 & Gunicorn - 502 错误网关

发布于 2025-01-14 00:40:52 字数 4717 浏览 1 评论 0原文

我在我的树莓派中运行了两个 Django 服务器,并通过 Gunicorn 和 Nginx 为它们提供服务。

其中一个运行正常,但另一个却给我带来了 502 Bad Gateway 问题。如果有人可以帮助我调试这个问题,我将不胜感激。

以下是我运行 sudojournalctl -uportfolio 时的日志:

 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

如果我检查 sudosystemctlstatusportfolio 一切都很好。

的方式:

portfolio.socketportfolio.service

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/portfolio.sock

[Install]
WantedBy=sockets.target

这是我配置portfolio.socket和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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

完美的未来在梦里 2025-01-21 00:40:52

解决了。

这是我在 /etc/nginx/sites-available 内的文件中的一个拼写错误

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文