woonizzooni

Docker - NGINX MEDIA SERVER 만들기 본문

Docker

Docker - NGINX MEDIA SERVER 만들기

woonizzooni 2020. 11. 8. 19:26

 

그냥 아래 파일을 주로 이용했었음. 개발용으로...

    hub.docker.com/r/tiangolo/nginx-rtmp/

    github.com/tiangolo/nginx-rtmp-docker/blob/master/Dockerfile

buildpack-deps:stretch를 기본 이미지로 사용했는데, 이미지 크기가 약 850MB임.

배포 이미지에 HLS, DASH가 비활성화 되어 있어서 필요할 경우 새로 빌드해야 했는데,

용량도 줄이고 옵션도 켜고 .. 나만의 이미지(?)를 만들기로 함. 

 

TL; DR

nginx-media-server : RTMP push스트림을 받아 RTMP/HLS/DASH 서빙.

    github.com/woonizzooni/nginx-media-server

 

  • 서버 실행
$ docker run -d -p 1935:1935 -p 8080:8080 --name nginx-media-server woonizzooni/nginx-media-server:latest

 

> Example
$ ffmpeg -f avfoundation -i "1:0" \
    -c:v libx264 -deinterlace -r 24 -s 1280x720 \
      -b:v 1200k -minrate 1200k -maxrate 1200k -bufsize 1200k -pix_fmt yuv420p \
      -profile:v baseline -x264-params keyint=48:keyint_min=24:scenecut=0:bframes=0 \
    -c:a libfdk_aac -b:a 128k -ar 44100 \
    -f flv "rtmp://localhost/live/test"
  • HLS/DASH 지원 플레이어로 아래 URL 재생
    HLS : http://localhost:8080/hls/test/index.m3u8
    DASH: http://localhost:8080/dash/test/index.m3u8

Details

 

Dockerfile

  - 옵션 변경이 필요할 경우 맘대로 하셈

FROM alpine:latest

LABEL maintainer="NGINX-MEDIA_SERVER maintainer <woonizzooni@gmail.com>"

# versions of nginx and nginx-rtmp-module to use
ENV NGINX_VERSION 1.18.0
ENV RTMP_MODULE_VERSION 1.2.1

RUN set -x \
# create nginx user/group
    && addgroup -g 101 -S nginx \
    && adduser -S -D -H -u 101 -h /var/cache/nginx -s /sbin/nologin -G nginx -g nginx nginx \
# install binaries & other dependencies from the published packaging sources
    && apk --no-cache add gcc libc-dev linux-headers pcre-dev zlib-dev ca-certificates openssl libressl-dev make \
    && rm -rf /var/cache/apk/*

# create working directory to build packages then go into.
RUN mkdir -p /tmp/build && cd /tmp/build

# download & decompress nginx & nginx-rtmp-module .tar.gz(gzipped tarball)
RUN wget -O nginx-${NGINX_VERSION}.tar.gz https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
    wget -O nginx-rtmp-module-${RTMP_MODULE_VERSION}.tar.gz https://github.com/arut/nginx-rtmp-module/archive/v${RTMP_MODULE_VERSION}.tar.gz && \
    tar -zxf nginx-${NGINX_VERSION}.tar.gz && \
    tar -zxf nginx-rtmp-module-${RTMP_MODULE_VERSION}.tar.gz

# build and install nginx with nginx-rtmp-module, clear working directory after build
# Edit this line if you want to add more modules or change options.
RUN cd nginx-${NGINX_VERSION} && \
    ./configure \
    --sbin-path=/usr/local/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx/nginx.lock \
    --http-log-path=/var/log/nginx/access.log \
    --http-client-body-temp-path=/tmp/nginx-client-body \
    --with-http_ssl_module \
    --with-cc-opt="-Wimplicit-fallthrough=0" \
    --with-file-aio \
    --with-threads \
    --add-module=../nginx-rtmp-module-${RTMP_MODULE_VERSION} && \
    make -j $(getconf _NPROCESSORS_ONLN) && \
    make install && \
    mkdir /var/lock/nginx && \
# clear working directory after build
    rm -rf /tmp/build

# forward access and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

# copy local nginx.conf to docker
COPY nginx.conf /etc/nginx/nginx.conf

# export 1935 for rtmp, 8080 for hls or dash
EXPOSE 1935 8080

CMD ["nginx", "-g", "daemon off;"]

 

nginx.conf

   - HLS, DASH 기능을 활성화 했고, 파일 저장 위치를 /tmp/hls, /tmp/dash로 했음.

   - 그 외 옵션은 'arut/nginx-rtmp-module' 참고

user nginx nginx;
worker_processes auto;
rtmp_auto_push on;
events {}
rtmp {
    server {
        listen 1935;
        listen [::]:1935 ipv6only=on;

        application live {
            live on;
            record off;

            hls on;
            hls_path /tmp/hls;
            hls_nested on;
            hls_fragment 2s;
            hls_playlist_length 16s;

            dash on;
            dash_path /tmp/dash;
            dash_nested on;
            dash_fragment 2s;
            dash_playlist_length 16s;

            # deny play all;
        }
    }
}

http {
    sendfile off;
    aio on;
    directio 512;
    default_type application/octet-stream;

    server {
        listen      8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this stylesheet to view XML as web page
            # in browser
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /path/to/stat.xsl/;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

        location /dash {
            # Serve DASH fragments
            root /tmp;
            add_header Cache-Control no-cache;
        }
    }
}

 

Comments