gpt4 book ai didi

python - 第一个请求不会终止(无 FIN)uWSGI + nginx

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:37 27 4
gpt4 key购买 nike

我使用 nginx 作为 uWSGI 服务器(flask 应用程序)前面的反向代理。

由于内存泄漏,请在多次调用后使用 --max-requests 重新加载工作线程。

问题如下:当工作人员刚刚重新启动/启动时,它收到的第一个请求在uWSGI和NGINX之间保持挂起状态,flask应用程序内部的处理时间通常并且非常快,但客户端会等待 uwsgi_send_timeout 被触发。

使用tcpdump查看请求(nginx是XXX.14,uWSGI是XXX.11):tcpdump communcation between nginx XXX.14 and uWSGI XXX.11

您可以在时间列中看到它挂起 300 秒(uwsgi_send_timeout),即使 NGINX 已收到 HTTP 请求... uWSGI 只是不发送 [FIN] 数据包来表示连接已关闭。然后NGINX触发超时并关闭 session 。

最终客户端收到一个被截断的响应。状态代码为 200。这非常令人沮丧。

无论请求有多大,每次工作人员重新加载时都会发生这种情况,仅一次,即第一个请求。

有人可以解决这个问题吗?我是否配置错误?

uwsgi.ini

[uwsgi]
# Get the location of the app
module = api:app

plugin = python3
socket = :8000
manage-script-name = true
mount = /=api:app
cache2 = name=xxx,items=1024

# Had to increase buffer-size because of big authentication requests.
buffer-size = 8192

## Workers management
# Number of workers
processes = $(UWSGI_PROCESSES)
master = true
# Number of requests managed by 1 worker before reloading (reload is time expensive)
max-requests = $(UWSGI_MAX_REQUESTS)

lazy-apps = true

single-interpreter = true

nginx-server.conf

server {
listen 443 ssl http2;
client_max_body_size 50M;

location @api {
include uwsgi_params;
uwsgi_pass api:8000;
uwsgi_read_timeout 300;
uwsgi_send_timeout 300;
}

最佳答案

出于某种奇怪的原因,在 nginx 配置中添加参数 uwsgi_buffering off; 解决了该问题。

我仍然不明白为什么,但现在这解决了我的问题。如果有人有有效的解释,请不要犹豫。

server {
listen 443 ssl http2;
client_max_body_size 50M;

location @api {
include uwsgi_params;
uwsgi_pass api:8000;
uwsgi_buffering off;
uwsgi_read_timeout 300;
uwsgi_send_timeout 300;
}

关于python - 第一个请求不会终止(无 FIN)uWSGI + nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com