【问题标题】:flask + nginx + uwsgi_response_sendfile_do() TIMEOUT烧瓶 + nginx + uwsgi_response_sendfile_do() 超时
【发布时间】:2015-02-25 15:30:42
【问题描述】:

我的烧瓶 + uwsgi + nginx 应用程序有问题,我无法下载大于 ~1GiB 的文件(大小每次相差几个字节)

  • 我的服务器上有大量可用内存
  • 我在服务器上有足够的可用硬盘空间
  • 我在 3 个不同的客户端上对其进行了测试,所以它不是客户端问题
  • 在 nginx 中使用静态别名时,我可以下载更大的文件(请参阅 nginx app.conf 中的 /cache && /repository)
  • 1 GiB 以下的文件可以正常工作
  • 我正在使用 flask.send_file 函数发送文件(参见 app.py)

我能找到的唯一错误是:

/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

【问题讨论】:

    标签: python nginx flask download uwsgi


    【解决方案1】:

    你应该根据documentation设置uwsgi_max_temp_file_size设置:

    当 uwsgi 服务器的响应缓冲被启用,并且整个响应不适合 uwsgi_buffer_size 和 uwsgi_buffers 指令设置的缓冲区时,可以将部分响应保存到临时文件中。该指令设置临时文件的最大大小。一次写入临时文件的数据大小由 uwsgi_temp_file_write_size 指令设置。

    此设置的默认值为 1024 MB。

    【讨论】:

    • 这真的帮助了我们。我敢打赌我的朋友们,如果这个解决方案有效,我会创建一个 stackoverflow 用户来支持它。所以我来了
    猜你喜欢
    • 2016-03-20
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多