【问题标题】:Nginx/Uwsgi/Flask POST times out if body is too large如果正文太大,Nginx/Uwsgi/Flask POST 超时
【发布时间】:2019-05-22 04:11:22
【问题描述】:

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

curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d '{"test": "test"}'

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

curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d @some_6mb_file.json

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

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

2018/12/21 20:47:45 [错误] 611#611: *1 上游超时 (110: 连接超时)同时从上游读取响应头, 客户端:10.4.3.168,服务器:,请求:“POST /model/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;

/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;
    }
}

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

client_max_body_size 128m;
client_body_buffer_size 128m;

【问题讨论】:

  • 愚蠢的问题,但你检查过 nginx 和 Uwsgi 的配置文件吗?也许他们只是有一个你需要增加的超时参数?
  • 我可能应该提到,虽然超时需要 60 秒才能显示,但应用程序会在一秒左右内停止处理。我认为问题在于 uwsgi 没有响应 nginx。
  • 你是如何启动你的容器的?
  • 你的 nginx 设置中的 client_max_body_size 是什么?默认为 1mb。
  • 尝试在 NGINX 配置中设置 uwsgi_read_timeout 和 uwsgi_send_timeout (nginx.org/en/docs/http/…)。

标签: python nginx flask uwsgi


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我在代理到 aiohttp (Python) 应用程序时遇到了这种行为。

    就我而言,在代理的位置块中,我需要禁用缓存:

    从区块中移除:

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    

    因此,工作配置是这样的:

      server {
        listen 80;
        location / {
            try_files $uri @app;
        }
        location @app {
          proxy_set_header Host $http_host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_redirect off;
          proxy_buffering off;
          proxy_pass http://myapp;
        }
        location /static {
            alias /app/static;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2011-10-04
      相关资源
      最近更新 更多