我正在尝试在NGINX访问日志中记录mp4文件的视频持续时间,这应该在客户端从服务器‘获取’mp4文件时发生。我已经配置了一个自定义日志格式,如下所示:
log_format nginx_custom_log '$remote_addr ... $request_uri $status $sent_http_content_type $video_duration';
access_log /var/log/nginx/access.log nginx_custom_log;
我可以添加一个自定义的header video_duration
,指向视频文件的路径并手动赋值,但这需要在每次添加视频并重新加载nginx时更改nginx的配置:
location /path/video.mp4 {
add_header video_duration 123456;
}
将以下记录写入NGINX访问日志:
192.168.0.1 ... /path/video.mp4 206 video/mp4 123456
我还尝试在X-Content-Duration
配置中配置NGINX头文件(Firefox不再支持它),但是没有记录任何值。
我找到了一个名为ngx_http_mp4_module的模块。它允许指定像?start=238.88&end=555.55
这样的参数,这让我相信NGINX能够读取mp4文件的元数据。
有没有办法在NGINX访问日志中记录mp4视频文件的时长,类似于记录文件的content-length
(字节)和content-type
(视频/mp4)的方式?
发布于 2021-01-12 19:11:46
下面是供参考的实现:
nginx.conf
location ~* \.(mp4)$ {
set $directoryPrefix '/usr/local/openresty/nginx/html';
set $metaFile '$directoryPrefix$request_uri.duration';
set $mp4_header "NOT_SET";
rewrite_by_lua_block {
local f = assert(io.open(ngx.var.metaFile, "r"))
ngx.var.mp4_header = f:read("*line")
f:close()
}
add_header mp4_duration $mp4_header;
}
log_format nginxlog '$remote_addr ... "$request_uri" $status $sent_http_content_type $sent_http_mp4_duration';
access_log /usr/local/openresty/nginx/logs/access.log nginxlog;
需要注意的是,$metaFile是指包含时长的元数据文件:
$directoryPrefix: /usr/local/openresty/nginx/html
$request_uri: /video/VideoABC.mp4
$metaFile: /usr/local/openresty/nginx/html/video/VideoABC.mp4.duration
access.log
192.168.0.1 ... "/video/VideoABC.mp4" 206 video/mp4 1234567890
元数据文件路径(&M)
root@ubuntu: cd /usr/local/openresty/nginx/html/video/
root@ubuntu: ls
VideoABC.mp4 VideoABC.mp4.duration
root@ubuntu: cat VideoABC.mp4.duration
1234567890
https://stackoverflow.com/questions/65606089
复制相似问题