【问题标题】:How to force download for video (mp4) files on Nginx based on query parameter如何根据查询参数在 Nginx 上强制下载视频(mp4)文件
【发布时间】:2019-02-18 20:37:00
【问题描述】:

我已经将它用于图像,但由于未知原因,它似乎不适用于视频 (mp4)。我添加了扩展 mp4,但它被忽略了。

我的代码;

location ~* ^/.+\.(?:gif|jpe?g|png|mp4)$ {
    autoindex off;
    if ($arg_dl = "1") {
        add_header Content-disposition "attachment; filename=$1";
    }
}

TIA

【问题讨论】:

    标签: nginx video download


    【解决方案1】:

    这可能是浏览器更关注Content-Type 而不是Content-Disposition 标头。您可以将Content-Type 设置为application/octet-stream 以强制浏览器下载文件。

    设置Content-Type 的正确方法是使用typesdefault_type 指令。但是这些指令不能出现在if 块中。详情请见this documentthis document

    我的解决方法是使用 /download/ 的内部 URI 前缀来实现附加逻辑:

    location ~* ^/.+\.(?:gif|jpe?g|png|mp4)$ {
        autoindex off;
        if ($arg_dl = "1") {
            rewrite ^(.*)$ /download$1 last;
        }
    }
    location ^~ /download {
        internal;
        alias /path/to/files;
        types {}
        expires -1;
        default_type application/octet-stream;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多