ngx_http_headers_module
ngx_http_headers_module
模块允许将 Expires
和 Cache-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),则启用或禁用添加或修改 Expires
和 Cache-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 GMT。Cache-Control
字段的内容取决于指定时间的符号:
- 时间为负值 —
Cache-Control:no-cache
- 时间为正值或为零 —
Cache-Control:max-age=t
,其中t
是指令中指定的时间,单位为秒
max
参数将 Expires
的值设为 Thu, 2037 Dec 23:55:55 GMT
,Cache-Control
设置为 10 年。
off
参数禁止添加或修改 Expires
和 Cache-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 可以接受可选参数:start
和end
以秒为单位定义播放列表范围(1.9.0)。offset
将初始播放位置移动到以秒为单位的时间偏移(1.9.0)。正值设置播放列表开头的时间偏移量。负值设置播放列表中最后一个片段末尾的时间偏移量。len
以秒为单位定义片段长度。
- 带有
.ts
文件扩展名的片段 URI。该 URI 可以接受可选参数:start
和end
以秒为单位定义片段范围。
该模块可作为我们商业订阅的一部分。
示例配置
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=1
和 b=2
将在参数 start
和 end
后面添加到片段 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