flask + nginx + uwsgi_response_sendfile_do() 超时

flask + nginx + uwsgi_response_sendfile_do() TIMEOUT

我的 flask + uwsgi + nginx 应用程序有问题,只是我无法下载比 ~1GiB 更大的文件(大小每次都相差几个字节)

我能找到的唯一错误是:

/var/log/uwsgi/app/updates.example.com.log

Wed Feb 25 15:48:31 2015 - uwsgi_response_sendfile_do() TIMEOUT !!!
IOError: write error
[pid: 22385|app: 0|req: 1/1] 94.113.167.6 () {42 vars in 961 bytes} [Wed Feb 25 16:19:22 2015] GET /download/file/63ac9e2a5952c1fbdccb143eec8769b6 => generated 0 bytes in 5606 msecs via sendfile() (HTTP/1.1 200) 6 headers in 261 bytes (3582 switches on core 0)

其他似乎都还好

问题在哪里?我缺少一些 chache/timeout 配置 ???

感谢任何帮助

nginx app.conf:

server {
    listen 80;
    server_name updates.example.com;
    root /home/user/updates.example.com;
    access_log /home/user/updates.example.com/logs/access.log;
    error_log /home/user/updates.example.com/logs/error.log;

    location /doc {
            index index.htm index.html;
            alias /home/user/updates.example.com/site;
    }

    #Testing static download dirs
    location /cache
    {
            alias /home/user/updates.example.com/cache;
    }

    location /repository
    {
            alias /home/user/updates.example.com/repository;
    }

    location / {
            uwsgi_pass unix:///home/user/updates.example.com/server.sock;
            include uwsgi_params;
    }
}

nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

app.py(简化举例)

from flask import Flask, jsonify, request, url_for, send_file, redirect

import os

app = Flask(__name__)

@app.route('/download/<string:sum>')
def download(sum):
    download_path = os.path.join(app.config['CACHE_DIR'], sum)

  if os.path.isfile(download_path):
    #everything seems to be ok
    #!FIXME this cannot be used, download crashes when file reaches 1Gib> using direct path in client now and special config in nginx 
    return send_file(download_path, attachment_filename=sum)


  return jsonify({ 'message': 'Task failed'}), 500

if __name__ == '__main__':
  app.run()

uwsgi app.ini

[uwsgi]
uid = user
logto = /home/user/updates.example.com/logs/uwsgi.log
master = true
chdir = /home/user/updates.example.com/
socket = server.sock
module = app
callable = app
plugins = python

#Fix for DB connection coruption
lazy = true
lazy-apps = true

您应该根据 documentation:

设置 uwsgi_max_temp_file_size 设置

When buffering of responses from the uwsgi server is enabled, and the whole response does not fit into the buffers set by the uwsgi_buffer_size and uwsgi_buffers directives, a part of the response can be saved to a temporary file. This directive sets the maximum size of the temporary file. The size of data written to the temporary file at a time is set by the uwsgi_temp_file_write_size directive.

此设置的默认值为 1024 MB。