如果正文太大,Nginx/Uwsgi/Flask POST 超时

Bra*_*ell 5 python nginx flask uwsgi

我正在使用基于https://github.com/tiangolo/uwsgi-nginx-flask-docker/tree/master/python3.6的 dockerimage 。我在里面运行一个 python 应用程序,它接受一个 POST,对 json 主体进行一些处理,并返回一个简单的 json 响应。像这样的帖子:

curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d '{"test": "test"}'
Run Code Online (Sandbox Code Playgroud)

工作正常。但是,如果我发布一个更大的 json 文件,我会收到 504: Gateway Timeout。

curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d @some_6mb_file.json
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,Nginx 和 Uwsgi 之间的通信存在问题,但我不知道如何解决。

编辑:我跳入 docker 容器并手动重新启动 nginx 以获得更好的日志记录。我收到以下错误:

2018/12/21 20:47:45 [错误] 611#611:*1 上游超时(110:连接超时),同时从上游读取响应头,客户端:10.4.3.168,服务器:,请求:“POST /模型/refuel_verification_model/predict HTTP/1.1”,上游:“uwsgi://unix:///tmp/uwsgi.sock”,主机:“10.4.3.168:5002”

从容器内部,我启动了 Flask 应用程序的第二个实例,在没有 Nginx 和 Uwsgi 的情况下运行,并且运行良好。响应需要大约 5 秒才能返回(由于数据的处理时间。)

配置:

/etc/nginx/nginx.conf:

user  nginx;
worker_processes 1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
daemon off;
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/conf.d/nginx.conf:

server {
    listen 80;
    location / {
        try_files $uri @app;
    }
    location @app {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
    location /static {
        alias /app/static;
    }
}
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/conf.d/upload.conf:

client_max_body_size 128m;
client_body_buffer_size 128m;
Run Code Online (Sandbox Code Playgroud)

Bra*_*ell 0

Tensorflow 存在问题。我在应用程序初始化期间加载了一个张量流模型,然后尝试稍后使用它。由于网络服务器完成的线程处理以及 Tensorflow 的“非线程安全”性质,处理挂起导致超时。