上游超时(uWSGI + NGINX + Flask)

Upstream timed out (uWSGI + NGINX + Flask)

我的应用程序有以下堆栈:

  1. NGINX (1.4.6)
  2. uWSGI(1.9.17.1-debian(64 位))
  3. 烧瓶
  4. Python 3.4

问题 NGINX 重启后一段时间(几分钟)一切正常。在那段时间之后,我收到“504 网关超时”错误。

NGINX 日志:

*13 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 86.123.39.44, server: app.fc.com, request: "GET /dashboard/overview HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "app.fc.com", referrer: "http://app.fc.com/dashboard/overview"

uWSGI日志:

app.fc.com [pid: 100017|app: 0|req: 103/219] 86.123.39.44 () {44 vars in 859 bytes} [Fri Mar 9 06:24:22 2018] GET /login => generated 245 bytes in 1 msecs (HTTP/1.1 302) 3 headers in 131 bytes (1 switches on core 1)

我的 NGINX 配置是:

server {
        listen 80;
        listen [::]:80;

        root /var/www/fc/website;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name app.fc.com;

        location / {
                sendfile        on;
                client_max_body_size 20M;
                keepalive_timeout  0;

                proxy_connect_timeout   10;
                proxy_send_timeout      15;
                proxy_read_timeout      20;

                include uwsgi_params;
                uwsgi_pass 127.0.0.1:8001;
                #uwsgi_read_timeout 30;
                #uwsgi_send_timeout 30;
                uwsgi_connect_timeout 60;
                uwsgi_ignore_client_abort on;
        }
}

我的uWSGI配置是:

[uwsgi]
vhost = true
socket = :8001
#wsgi-file = /var/www/app.fc.com/reviewApp/wsgi.py
wsgi-file = /var/www/fc/app/wsgi.py
callable = app
processes = 2
threads = 4
chdir = /var/www/fc/app/

pythonpath = /var/www/fc/app/
pythonpath = /var/www/py3/lib/python3.4
virtualenv = /var/www/py3
plugins = python3

通常,对我的请求的响应持续不到 5 秒

解决方法

我只是在调用 Redis 的页面上发现了这种行为。我通过在 Redis 为每个请求添加一个新连接并在 Redis 配置中设置连接空闲超时来解决(短期解决方案)。

其他发现

当连接在许多 uWSGI 线程之间共享时,似乎 Redis 在 300 秒不活动(这是出现此问题后的间隔)后挂起约 931 秒。