视图加载时间超过 5 秒时出现 504 错误

504 error when the view loading takes more than 5 s

我安装了 django-nginx-gunicorn-supervisor-postgresql。在 Django 管理员中,如果视图加载时间超过 5 秒,我会收到 504。

例如,如果我筛选包含许多记录的更改列表视图并且花费的时间超过该时间,则会出现 504。只要花费更少的时间,具有更少记录的相同视图就可以工作。

我还注意到一些视图仍然 运行 在后台,即使在 504 之后,因为我可以看到它们完成后在数据库中所做的修改。

我尝试增加我发现的所有超时(nginx、gunicorn),但其中 none 配置为 5 秒。 猜猜什么可能配置错误?或者引发 504 的超时可能在哪里?

谢谢

我配置的超时是:

在/etc/nginx/sites-enabled/mysite

proxy_connect_timeout 60;
proxy_read_timeout 90;
proxy_send_timeout 90;
send_timeout 90;
fastcgi_read_timeout 300;

在/etc/nginx/nginx.conf

send_timeout 300;
keepalive_timeout 65;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;

在 gunicorn_start 文件中(由主管启动)

exec gunicorn -c ${CONF_FILE} ${DJANGO_WSGI_MODULE}:application \
        --name ${NAME} \
        --user=${USER} --group=${GROUP} \
        --log-level=debug \
        --timeout=90

在gunicorn.conf文件中(前${CONF_FILE})

timeout = 90
graceful_timeout = 30
keepalive = 3

我发现调试事物的最佳方法是从 Django 开始,然后逐步进行调试,直到找出超时的原因。

首先,检查 Django 是否超时。

在一个终端中运行:

python manage.py runserver 127.0.0.1:8000

然后在同一台机器上的终端中执行:

wget http://127.0.0.1:8000/<path_to_your_admin_view>

如果可行,则检查 Gunicorn 是否超时:

在 Gunicorn 配置文件中,更改设置,使其绑定到本地端口而不是套接字:

exec gunicorn -c ${CONF_FILE} ${DJANGO_WSGI_MODULE}:application \
        --name ${NAME} \
        --user=${USER} --group=${GROUP} \
        --log-level=debug \
        --bind=127.0.0.1:8001
        --timeout=90

确保重新启动主管。然后在同一台机器上的终端中执行:

 wget http://127.0.0.1:8001/<path_to_your_admin_view>

如果可行,则撤消对 gunicorn 配置的更改,重新启动 supervisor 并直接连接到 nginx 以查看它是否超时:

 wget http://127.0.0.1:80/<path_to_your_admin_view>

如果这有效,您的问题可能出在代理或负载平衡器的上游。