[英]How to serve part of a file with Nginx?
I'm using X-Accel to serve protected content, using X-Accel-Redirect.我正在使用X-Accel提供受保护的内容,使用 X-Accel-Redirect。
Is it possible to serve only a part of the file?是否可以只提供文件的一部分? for example, bytes range 0-x, or first 5 minutes of a video (my final goal)例如,字节范围为 0-x,或视频的前 5 分钟(我的最终目标)
It's important to do that on the server-side, so the client will not have access to the rest of the file.在服务器端执行此操作很重要,因此客户端将无法访问文件的 rest。
Currently this is how I send the whole file:目前这是我发送整个文件的方式:
X-Accel-Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/octet-stream
Content-Length: {file_size}
Content-Disposition: attachment; filename="myfile.mp4"
Accept-Ranges: bytes
X-Accel-Buffering: yes
X-Accel-Redirect: /protected/myfile.mp4
Nginx conf: Nginx 配置:
location /protected {
internal;
alias /dir/of/protected/files/;
if_modified_since off;
output_buffers 2 1m;
open_file_cache max=50000 inactive=10m;
open_file_cache_valid 15m;
open_file_cache_min_uses 1;
open_file_cache_errors off;
}
The massive hack would be to use nginx to proxy to itself with a Range
header that would limit the request to a range of bytes大规模的黑客攻击将是使用 nginx 以Range
header 将请求限制为字节范围
something like this (not tested so this probably wont work, but the idea should work):像这样的东西(未经测试,所以这可能行不通,但这个想法应该可行):
{
... snip config ...
server {
listen 80 default_server;
listen [::]:80 default_server;
root /html;
index index.html;
location / {
proxy_pass http://localhost/content;
add_header Range btyes=0,100000;
}
location /content {
try_files $uri $uri/ =404;
}
}
}
I haven't tested Slice
and X-Accel
together.我还没有一起测试过Slice
和X-Accel
。 If each file can have a different limit defined by the backend you might configure Slice in the location and send the limit with the X-Accel-Redirect
URL as below:如果每个文件可以具有由后端定义的不同限制,您可以在该位置配置Slice并使用X-Accel-Redirect
URL 发送限制,如下所示:
X-Accel-Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Type: application/octet-stream
Content-Length: {file_size}
Content-Disposition: attachment; filename="myfile.mp4"
Accept-Ranges: bytes
X-Accel-Buffering: yes
X-Accel-Redirect: /protected/myfile.mp4?s=0&e=$PHP_VAR
Nginx.conf Nginx.conf
location /protected {
slice; # enable slicing
slice_start_arg s;
slice_end_arg e;
internal;
alias /dir/of/protected/files/;
if_modified_since off;
output_buffers 2 1m;
open_file_cache max=50000 inactive=10m;
open_file_cache_valid 15m;
open_file_cache_min_uses 1;
open_file_cache_errors off;
}
A global file limit全局文件限制
You would need to redirect the original request including the Slice parameters to truncate the file being served.您需要重定向包含 Slice 参数的原始请求以截断正在提供的文件。
Nginx conf: Nginx 配置:
location /sample {
slice; # enable slicing
slice_start_arg s;
slice_end_arg e;
internal;
alias /dir/of/protected/files/;
if_modified_since off;
output_buffers 2 1m;
open_file_cache max=50000 inactive=10m;
open_file_cache_valid 15m;
open_file_cache_min_uses 1;
open_file_cache_errors off;
}
location /protected {
rewrite ^ /sample&s=0&e=1024; # replace for the desired file limit in bytes
}
If the rewrite
directive above doesn't work, I suggest the following option using proxy_pass
.如果上面的rewrite
指令不起作用,我建议使用以下选项proxy_pass
。
location /protected {
set $file_limit 1024 # replace for the desired file limit in bytes
set $delimiter "";
if ($is_args) {
set $delimiter "&";
}
set $args $args${delimiter}s=0&e=$file_limit;
proxy_pass $scheme://127.0.0.1/sample;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.