django无法使用502 Bad Gateway记录数据库连接问题

时间:2019-12-25 04:13:01

标签: django nginx gunicorn

在尝试诊断502 Bad Gateway错误时,我迅速确定这是数据库连接问题(因为我刚刚更新了host中的settings.py条目),但是没有一个日志文件指出了任何可能导致数据库连接问题的东西...

tail /var/log/nginx/error.log

2019/12/25 03:53:13 [error] 818#818: *11 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "example.com"
2019/12/25 03:56:54 [error] 818#818: *14 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "example.com"
2019/12/25 03:58:08 [error] 818#818: *16 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "example.com"

tail /var/www/example.com/example/gunicorn.error.log(gunicorn错误日志文件写入的位置)

[2019-12-25 03:56:56 +0000] [2447] [INFO] Starting gunicorn 20.0.4
[2019-12-25 03:56:56 +0000] [2447] [INFO] Listening at: unix:/run/gunicorn.sock (2447)
[2019-12-25 03:56:56 +0000] [2447] [INFO] Using worker: sync
[2019-12-25 03:56:56 +0000] [2474] [INFO] Booting worker with pid: 2474
[2019-12-25 03:56:56 +0000] [2475] [INFO] Booting worker with pid: 2475
[2019-12-25 03:56:56 +0000] [2477] [INFO] Booting worker with pid: 2477
[2019-12-25 03:56:56 +0000] [2478] [INFO] Booting worker with pid: 2478
[2019-12-25 03:56:57 +0000] [2479] [INFO] Booting worker with pid: 2479
[2019-12-25 03:58:07 +0000] [2447] [CRITICAL] WORKER TIMEOUT (pid:2478)
[2019-12-25 03:58:08 +0000] [2495] [INFO] Booting worker with pid: 2495

journalctl -u gunicorn.service

-- Logs begin at Wed 2019-12-25 03:55:53 UTC, end at Wed 2019-12-25 04:06:10 UTC. --
Dec 25 03:56:20 gunicorn systemd[1]: Stopping gunicorn daemon...
Dec 25 03:56:20 gunicorn systemd[1]: Stopped gunicorn daemon.
Dec 25 03:56:20 gunicorn systemd[1]: Started gunicorn daemon.
Dec 25 03:56:56 gunicorn systemd[1]: Stopping gunicorn daemon...
Dec 25 03:56:56 gunicorn systemd[1]: Stopped gunicorn daemon.
Dec 25 03:56:56 gunicorn systemd[1]: Started gunicorn daemon.

什么也没有说“无法连接到数据库...”

我缺少要检查的日志文件吗?

我应该关注某个特定的settings.py条目还是仔细检查其值?

django不会在可见的情况下记录数据库连接问题,这很奇怪。将来如果我的数据库连接断开,我希望这很明显。

编辑:在upstream prematurely closed connection while reading response header from upstream” Django, Ubuntu, Nginx, Gunicorn中查看此答案后,我猜测发生的是我的数据库连接超时> 30秒,因此gunicorn的停留时间不够长记录任何东西...

但是通过添加...

'OPTIONS': {
   'connect_timeout': 15,

转到DATABASES中的settings.py条目,现在我在gunicorn.error日志中看到错误

0 个答案:

没有答案