上游超时 (uWSGI + NGINX + Flask)

3
我为我的应用程序配置了以下堆栈:
  1. NGINX (1.4.6)
  2. uWSGI (1.9.17.1-debian (64bit))
  3. Flask
  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秒。

你有查看uwsgi日志吗? - heemayl
@heemayl 我已经添加了来自uWSGI的日志 - Adrian B
如果您从设置中删除uWSGI并直接使用NGINX提供Flask应用程序,会怎样呢?这只是为了帮助找到罪魁祸首。 - mehdix
1个回答

1
解决方案 我发现这种行为只出现在调用Redis的页面上。我通过为每个请求添加一个新的Redis连接并在Redis配置中设置连接空闲超时来解决了这个问题(短期解决方案)。 其他发现 当连接在多个uWSGI线程之间共享时,似乎Redis在300秒不活动后挂起约931秒(即此问题出现的间隔)。

同样的问题。我在beanstalkd连接方面有问题。 - Hảo Nguyễn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接