Nginx官方文档(二十)【ngx_http_headers_module|ngx_http_hls_module】

ngx_http_headers_module

ngx_http_headers_module 模块允许将 ExpiresCache-Control 头字段以及任意字段添加到响应头中。

示例配置

expires    24h;
expires    modified +24h;
expires    @24h;
expires    0;
expires    -1;
expires    epoch;
expires    $expires;
add_header Cache-Control private;

指令

add_header

-说明
语法add_header name value [always];
默认——
上下文http、server、location、location 中的 if

如果响应代码等于 200、201(1.3.10)、204、206、301、302、303、304、307(1.1.16、1.0.13)或 308(1.13.0),则将指定的字段添加到响应报头中。该值可以包含变量。

可以存在几个 add_header 指令。当且仅当在当前级别上没有定义 add_header 指令时,这些指令才从上一级继承。

如果指定了 always 参数(1.7.5),则无论响应代码为何值,头字段都将被添加。

add_trailer

-说明
语法add_trailer number size;
默认——
上下文http、server、location、location 中的 if
提示该指令在 1.13.2 版本中出现

如果响应代码等于 200、201、206、301、302、303、307 或 308,则将指定的字段添加到响应的末尾。该值可以包含变量。

可以存在多个 add_trailer 指令。当且仅当在当前级别上没有定义 add_trailer 指令时,这些指令才从上一级继承。

如果指定 always 参数,则无论响应代码为何值,都会添加指定的字段。

expires

-说明
语法expires [modified] time;
expires epoch |max | off;
默认expires off;
上下文http、server、location、location 中的 if

如果响应代码等于 200、201(1.3.10)、204、206、301、302、303、304
307(1.1.16、1.0.13)或 308(1.13.0),则启用或禁用添加或修改 ExpiresCache-Control 响应头字段。参数可以是正值或负值。

``Expires字段中的时间计算为指令中指定的time和当前时间的总和。如果使用modified参数(0.7.0、0.6.32),则计算时间为文件修改时间与指令中指定的time` 之和。

另外,可以使用 @ 前缀指定一天的时间(0.7.9、0.6.34):

expires @15h30m;

epoch 参数对应于绝对时间 Thu, 01 Jan 1970 00:00:01 GMTCache-Control 字段的内容取决于指定时间的符号:

  • 时间为负值 — Cache-Control:no-cache
  • 时间为正值或为零 — Cache-Control:max-age=t,其中 t 是指令中指定的时间,单位为秒

max 参数将 Expires 的值设为 Thu, 2037 Dec 23:55:55 GMTCache-Control 设置为 10 年。

off 参数禁止添加或修改 ExpiresCache-Control 响应头字段。

最后一个参数值可以包含变量(1.7.9):

map $sent_http_content_type $expires {
    default         off;
    application/pdf 42d;
    ~image/         max;
}

expires $expires;

原文档

http://nginx.org/en/docs/http/ngx_http_headers_module.html


ngx_http_headers_module

ngx_http_hls_module 模块为 MP4 和 MOV 媒体文件提供 HTTP Live Streaming(HLS)服务器端支持。这些文件通常具有 .mp4.m4v.m4a.mov.qt 扩展名。该模块支持 H.264 视频编解码器、AAC 和 MP3 音频编解码器。

对于每个媒体文件,支持两种 URI:

  • 带有 .m3u8 文件扩展名的播放列表 URI。该 URI 可以接受可选参数:
    • startend 以秒为单位定义播放列表范围(1.9.0)。
    • offset 将初始播放位置移动到以秒为单位的时间偏移(1.9.0)。正值设置播放列表开头的时间偏移量。负值设置播放列表中最后一个片段末尾的时间偏移量。
    • len 以秒为单位定义片段长度。
  • 带有 .ts 文件扩展名的片段 URI。该 URI 可以接受可选参数:
    • startend 以秒为单位定义片段范围。

该模块可作为我们商业订阅的一部分。

示例配置

location / {
    hls;
    hls_fragment            5s;
    hls_buffers             10 10m;
    hls_mp4_buffer_size     1m;
    hls_mp4_max_buffer_size 5m;
    root /var/video/;
}

在此配置中,/var/video/test.mp4 文件支持以下 URI:

http://hls.example.com/test.mp4.m3u8?offset=1.000&start=1.000&end=2.200
http://hls.example.com/test.mp4.m3u8?len=8.000
http://hls.example.com/test.mp4.ts?start=1.000&end=2.200

指令

hls

-说明
语法hls;
默认——
上下文location

为当前 location 打开 HLS 流。

hls_buffers

-说明
语法hls_buffers number size;
默认hls_buffers 8 2m;
上下文http、server、location

设置用于读取和写入数据帧的缓冲区的最大数量(number)和大小(size)。

hls_forward_args

-说明
语法hls_forward_args on | off;
默认hls_forward_args off;
上下文http、server、location
提示该指令在 1.5.12 版本中出现

将播放列表请求中的参数添加到片段的 URI 中。这对于在请求片段时或在使用 ngx_http_secure_link_module 模块保护 HLS 流时执行客户端授权非常有用。

例如,如果客户端请求播放列表 http://example.com/hls/test.mp4.m3u8?a=1&b=2,参数 a=1b=2 将在参数 startend 后面添加到片段 URI 中:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:15
#EXT-X-PLAYLIST-TYPE:VOD

#EXTINF:9.333,
test.mp4.ts?start=0.000&end=9.333&a=1&b=2
#EXTINF:7.167,
test.mp4.ts?start=9.333&end=16.500&a=1&b=2
#EXTINF:5.416,
test.mp4.ts?start=16.500&end=21.916&a=1&b=2
#EXTINF:5.500,
test.mp4.ts?start=21.916&end=27.416&a=1&b=2
#EXTINF:15.167,
test.mp4.ts?start=27.416&end=42.583&a=1&b=2
#EXTINF:9.626,
test.mp4.ts?start=42.583&end=52.209&a=1&b=2

#EXT-X-ENDLIST

如果 HLS 流受到 ngx_http_secure_link_module 模块的保护,则不应在 secure_link_md5 表达式中使用 $uri,因为这会在请求片段时触发错误。应该使用 Base URI 而不是 $uri(在示例中为 $hls_uri):

http {
    ...

    map $uri $hls_uri {
        ~^(?<base_uri>.*).m3u8$ $base_uri;
        ~^(?<base_uri>.*).ts$   $base_uri;
        default                 $uri;
    }

    server {
        ...

        location /hls {
            hls;
            hls_forward_args on;

            alias /var/videos;

            secure_link $arg_md5,$arg_expires;
            secure_link_md5 "$secure_link_expires$hls_uri$remote_addr secret";

            if ($secure_link = "") {
                return 403;
            }

            if ($secure_link = "0") {
                return 410;
            }
        }
    }
}

hls_fragment

-说明
语法hls_fragment time;
默认hls_fragment 5s;
上下文http、server、location

定义未使用 len 参数请求的播放列表 URI 的默认片段长度。

hls_mp4_buffer_size

-说明
语法hls_mp4_buffer_size size;
默认hls_mp4_buffer_size 512k;
上下文http、server、location

设置用于处理 MP4 和 MOV 文件的缓冲区的初始大小(size)。

hls_mp4_max_buffer_size

-说明
语法hls_mp4_max_buffer_size size;
默认hls_mp4_max_buffer_size 10m;
上下文http、server、location

在元数据处理期间,可能需要更大的缓冲区。其大小不能超过指定的大小(size),否则 nginx 将返回 500 状态码(内部服务器错误),并记录以下消息:

"/some/movie/file.mp4" mp4 moov atom is too large:
12583268, you may want to increase hls_mp4_max_buffer_size

原文档

http://nginx.org/en/docs/http/ngx_http_hls_module.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Nginx 中安装 ngx_http_mp4_module 模块,需要按照以下步骤进行操作: 1. 确认 Nginx 是否支持 ngx_http_mp4_module 模块 首先需要确认 Nginx 是否支持 ngx_http_mp4_module 模块,可以使用以下命令查看 Nginx 是否已经编译了该模块: ``` nginx -V 2>&1 | grep -o with-http_mp4_module ``` 如果输出结果为 with-http_mp4_module,则说明 Nginx 已经编译了 ngx_http_mp4_module 模块;如果输出结果为空,则说明 Nginx 没有编译该模块。 2. 下载 ngx_http_mp4_module 模块 如果 Nginx 没有编译 ngx_http_mp4_module 模块,需要下载该模块并添加到 Nginx 中。可以从 Github 上下载该模块,链接为:https://github.com/kaltura/nginx-vod-module。 可以使用以下命令将 ngx_http_mp4_module 模块下载到 /opt 目录下: ``` cd /opt git clone https://github.com/kaltura/nginx-vod-module.git ``` 3. 编译 Nginx 并添加 ngx_http_mp4_module 模块 在编译 Nginx 时需要添加 --add-module=/opt/nginx-vod-module 参数来指定 ngx_http_mp4_module 模块所在的目录,具体命令如下: ``` ./configure --prefix=/usr/local/nginx --add-module=/opt/nginx-vod-module make make install ``` 4. 配置 NginxNginx 的配置文件中添加以下内容,即可使用 ngx_http_mp4_module 模块: ``` location /video/ { mp4; mp4_buffer_size 1m; mp4_max_buffer_size 5m; } ``` 其中,/video/ 是视频文件所在的目录。mp4ngx_http_mp4_module 模块提供的指令,表示该目录下的文件都是 MP4 格式的视频文件。 mp4_buffer_size 和 mp4_max_buffer_size 是 ngx_http_mp4_module 模块提供的两个参数,用于控制视频文件的缓存大小。 5. 重启 Nginx 完成以上步骤后,需要重启 Nginx 使配置生效: ``` nginx -s reload ``` 至此,ngx_http_mp4_module 模块安装完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值