![Nginx - 启用多媒体文件的下载而不是流式传输?](https://linux22.com/image/746570/Nginx%20-%20%E5%90%AF%E7%94%A8%E5%A4%9A%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8B%E8%BD%BD%E8%80%8C%E4%B8%8D%E6%98%AF%E6%B5%81%E5%BC%8F%E4%BC%A0%E8%BE%93%EF%BC%9F.png)
我正在尝试在新设置的服务器上启用多媒体(Mp3 和 Mp4)文件下载,nginx/1.17.6
但HTTP2
文件开始流式传输而不是下载。这是我的nginx.conf
文件:
user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
worker_rlimit_nofile 4096;
events {
worker_connections 4096;
multi_accept on;
# essential for linux, optmized to serve many clients with each thread
use epoll;
}
http {
include /etc/nginx/mime.types;
## Performance
## access_log off;
access_log /var/log/nginx/access.log;
open_file_cache max=10000 inactive=1200s;
open_file_cache_valid 1200s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
##Security
#limit_req_zone $binary_remote_addr zone=one:30m rate=30r/m; #Allow 30 requests per minute (2 req per sec), check location tag for forcing policy
#limit_conn_zone $binary_remote_addr zone=two:30m; #Limit the number of simultaneous connections from a single IP, check location tag for forcing policy
#limit_conn_log_level warn;
add_header X-Content-Type-Options nosniff;
sendfile on;
sendfile_max_chunk 5m;
tcp_nopush on;
keepalive_timeout 90;
tcp_nodelay on;
gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
charset utf-8;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/conf.d/stub_status.conf;
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;
send_timeout 280s;
}
我使用下面的 php 代码来启动下载:
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("X-Accel-Redirect: /vtemp/" . $fname); //This is nginx specific, eqv. to XSendfile
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fName\"");
header("Content-Transfer-Encoding: binary");
header('Connection: close');
Pagespeed
模块也已启用(这可能是一个问题吗?因为我的其他工作服务器上未启用该模块):
pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
相同的配置在我的另一台服务器上有效,该服务器具有旧的 nginx 构建并且不使用HTTP2
协议,我在这里遗漏了什么?
答案1
尝试将其添加到您的 nginx.confserver
部分:
location ~* (./?|\mp3|\mp4)$ {
types { application/octet-stream (.mp3|.mp4); }
default_type application/octet-stream;
}
答案2
添加后,pagespeed Disallow "*/v3/*.php*";
我的问题得到了解决。