非常教程

Nginx参考手册

ngx_http_hls_module

ngx_http_hls_module

  • 示例配置
  • 指令
  • HLS
  • hls_buffers
  • hls_forward_args
  • hls_fragment
  • hls_mp4_buffer_size
  • hls_mp4_max_buffer_size

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流。

句法:

hls_buffers数字大小;

默认:

hls_buffers 8 2m;

语境:

http,服务器,位置

设置用于读写数据帧的最大值numbersize缓冲区。

句法:

hls_forward_args 开启 | 关闭;

默认:

hls_forward_args关闭;

语境:

http,服务器,位置

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

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

#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模块保护,则$uri不应在secure_link_md5表达式中使用,因为这会在请求片段时导致错误。应该使用基本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 5s;

语境:

http,服务器,位置

定义不带“ len”参数的播放列表URI的默认片段长度。

句法:

hls_mp4_buffer_size大小;

默认:

hls_mp4_buffer_size 512k;

语境:

http,服务器,位置

设置size用于处理MP4和MOV文件的缓冲区的初始值。

句法:

hls_mp4_max_buffer_size大小;

默认:

hls_mp4_max_buffer_size 10m;

语境:

http,服务器,位置

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

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

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module