【问题标题】:Flask app behind gunicorn and nginx is only streaming files, not allowing for downloadgunicorn 和 nginx 后面的 Flask 应用程序只是流文件,不允许下载
【发布时间】:2021-07-22 07:47:18
【问题描述】:

我目前在子域上有一个应用程序,名为 api.example.com,并且我有 example.com 将带有 api.example.com 的文件放入 HTML 流文件中,而不是发送它以供下载。

HTML 看起来是这样的

<a href="https://api.example.com/path/to/file" download="https://api.example.compath/to/file">

我也试过了 &lt;a href="https://api.example.com/path/to/file" download&gt;

我的烧瓶路线是这样的


CORS(app, headers='Content-Type')

@app.route('/static/downloads/<path:path>')
def generic_send_file(path):
    final_name = path.split('/')[-1]

    return send_from_directory(
        app.config['DOWNLOAD_FOLDER'],
        path,
        as_attachment=True,
        attachment_filename=final_name
    )

我的 nginx 路由是这样的

    server_name api.example.com;
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        alias  /home/www/api/static/;
    }
    client_max_body_size 100000m;

这是我的独角兽

[program:api]
command = /home/www/env/bin/gunicorn run:app -b localhost:8000
environment=PATH="/home/www/env/bin:%(ENV_PATH)s"
directory = /home/www/api
user = api
stdout_logfile = /var/log/api/api.out.log
stderr_logfile = /var/log/api/api.err.log

flask 应用在本地运行。但是当我把它上线时,它不是发送 mp3 文件来下载,而是在浏览器中打开它。

任何帮助将不胜感激。

【问题讨论】:

    标签: nginx flask gunicorn


    【解决方案1】:

    我不得不把它放到 nginx 路由中

        location /static/downloads/  {
                    autoindex on;
                    alias /home/www/api/static/downloads/;
                    expires 30d;
                            if ( $request_filename ~ "^.*/(.+.*)$" ){
                            set $fname $1;
                            add_header Content-Disposition 'attachment; filename="$fname"';
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2019-10-03
      • 2020-06-21
      • 2021-05-12
      相关资源
      最近更新 更多