본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

ngx_http_hls_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"는 프래그먼트 경계를 초 단위로 정의해요.

이 모듈은 상용 구독(commercial subscription)의 일부로 제공돼요.

출처: 문서

본문

예시 구성 (Example Configuration)

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

지시어 (Directives)

Syntax: hls;
Default: —
Context: location

주변 location에서 HLS 스트리밍을 켜요.

Syntax: hls_buffers number size;
Default: hls_buffers 8 2m;
Context: http, server, location

데이터 프레임을 읽고 쓰는 데 사용되는 버퍼의 최대 개수와 크기를 설정해요.

Syntax: hls_forward_args on | off;
Default: hls_forward_args off;
Context: 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를 사용하면 프래그먼트를 요청할 때 오류가 발생하므로 $uri를 사용하면 안 돼요. $uri 대신 기준(base) 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;
            }
        }
    }
}
Syntax: hls_fragment time;
Default: hls_fragment 5s;
Context: http, server, location

"len" 인수 없이 요청된 플레이리스트 URI에 대한 기본 프래그먼트 길이를 정의해요.

Syntax: hls_mp4_buffer_size size;
Default: hls_mp4_buffer_size 512k;
Context: http, server, location

MP4와 MOV 파일을 처리하는 데 사용되는 버퍼의 초기 크기를 설정해요.

Syntax: hls_mp4_max_buffer_size size;
Default: hls_mp4_max_buffer_size 10m;
Context: http, server, location

메타데이터 처리 중에는 더 큰 버퍼가 필요할 수 있어요. 그 크기는 지정된 크기를 초과할 수 없는데, 그렇지 않으면 nginx가 서버 오류 500 (Internal Server Error)을 반환하고 다음 메시지를 로그로 남겨요:

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

더 알아보기 (Learn more)