我使用nginx作为uWSGI服务器(flask应用程序)前的反向代理。
由于内存泄漏,请在多次调用后使用--max-requests
重新加载工作进程。
问题是:当一个worker刚刚重启/启动时,它收到的第一个请求在uWSGI和NGINX之间保持挂起,flask应用程序内部的处理时间是正常的并且非常快,但是客户端等待直到uwsgi_send_timeout
被触发。
使用tcpdump查看请求(nginx为XXX.14,uWSGI为XXX.11):
您可以在时间列中看到它挂起了300秒(uwsgi_send_timeout),即使HTTP请求已经被NGINX...uWSGI只是不发送FIN数据包来表示连接已关闭。然后NGINX触发超时并关闭会话。
终端客户端收到截断的响应。状态代码为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;
}
发布于 2019-03-12 03:43:51
出于某种奇怪的原因,在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;
}
https://stackoverflow.com/questions/55089152
复制相似问题