nginx+rtmp+yamdi镜像制作

基于alpine:3.14镜像制作nginx镜像,支持rtmp协议,直播录像保存,并添加metadata支持进度条。

nginx下载地址:http://nginx.org/en/download.html

nginx-http-flv-module下载地址:https://github.com/winshining/nginx-http-flv-module

yamdi下载地址:https://sourceforge.net/projects/yamdi/files/yamdi/1.9/yamdi-1.9.tar.gz/download

创建dev镜像

FROM alpine:3.14

LABEL MAINTAINER huhongbin

RUN echo "https://mirrors.aliyun.com/alpine/v3.14/main/" > /etc/apk/repositories && \
    echo "https://mirrors.aliyun.com/alpine/v3.14/community/" >> /etc/apk/repositories

RUN apk update \
    # ca-certificates访问https时需要
    && apk add --no-cache ca-certificates curl git \
    && apk add --no-cache --virtual .build-deps \
        gcc \
        libc-dev \
        g++ \
        gdb \
        make \
        cmake \
        openssl-dev \
        pcre-dev \
        zlib-dev \
        linux-headers \
        libxslt-dev \
        gd-dev \
        geoip-dev \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

创建镜像

docker build -t alpine:jc-dev .

创建nginx镜像

进入目录,目录下文件如下图

[root@harbor http-flv]# ll -a
total 16
drwxr-xr-x 5 root root  123 Oct  9 13:50 .
drwxr-xr-x 5 root root  129 Oct  9 09:22 ..
-rw-r--r-- 1 root root 3181 Oct  9 11:23 Dockerfile
drwxr-xr-x 9 root root  186 Oct  7 16:46 nginx-1.14.2
-rw-r--r-- 1 root root 3963 Oct  9 11:28 nginx.conf
drwxr-xr-x 8 root root 4096 Oct  7 16:47 nginx-http-flv-module
drwxr-xr-x 3 root root  117 Oct  9 09:33 yamdi-1.9

Dockerfile如下:

FROM alpine:jc-dev AS builder

LABEL MAINTAINER huhongbin


# 指定nginx和yamdi的位置
ARG NGINX_PATH=/usr/local/nginx
ARG YAMDI_PATH=/usr/local/bin
ARG NGINX_VERSION=nginx-1.14.2
ARG YAMDI_VERSION=yamdi-1.9
ADD ./  /

# 编译安装nginx
RUN CONFIG="\
        --prefix=$NGINX_PATH \
        --add-module=/nginx-http-flv-module \
        --with-http_flv_module \
        --with-http_mp4_module" \
    && cd /$NGINX_VERSION \
    && ./configure $CONFIG --with-debug \
    && make -j$(getconf _NPROCESSORS_ONLN) \
    && make install \
    # 缩小文件大小
    && strip $NGINX_PATH/sbin/nginx* \
    # 获取nginx运行需要的库(下面的打包阶段无法获取,除非写死)
    && runDeps="$( \
        scanelf --needed --nobanner $NGINX_PATH/sbin/nginx  /tmp/envsubst \
            | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
            | sort -u \
            | xargs -r apk info --installed \
            | sort -u \
    )" \
    # 保存到文件,给打包阶段使用
    && echo $runDeps >> $NGINX_PATH/nginx.depends \
    && cd /${YAMDI_VERSION} && make && make install \
    && yamdiDeps="$( \
        scanelf --needed --nobanner $YAMDI_PATH_PATH/yamdi  /tmp/envsubst \
            | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
            | sort -u \
            | xargs -r apk info --installed \
            | sort -u \
    )" \
    && echo $yamdiDeps >> $NGINX_PATH/nginx.depends

FROM alpine:3.14

LABEL MAINTAINER huhongbin

# 指定nginx和yamdi的位置
ARG NGINX_PATH=/usr/local/nginx
ARG YAMDI_PATH=/usr/local/bin

# 定义一个环境变量,方便后面运行时可以进行替换
ENV NGINX_CONF /usr/local/nginx/conf/nginx.conf

# 从build中拷贝编译好的文件
COPY --from=Builder $NGINX_PATH $NGINX_PATH
COPY --from=Builder $YAMDI_PATH/yamdi $YAMDI_PATH/


# 将目录下的文件copy到镜像中(默认的配置文件)
COPY nginx.conf $NGINX_CONF


# 修改源及添加用户
RUN echo "https://mirrors.aliyun.com/alpine/v3.14/main/" > /etc/apk/repositories \
    && echo "https://mirrors.aliyun.com/alpine/v3.14/community/" >> /etc/apk/repositories \
    # 添加nginx运行时的用户
    && addgroup -S nginx \
    && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
    && mkdir $NGINX_PATH/live_record \
    && mkdir $NGINX_PATH/video \
    && ln -sf /dev/stdout /usr/local/nginx/logs/access.log \
    && ln -sf /dev/stderr /usr/local/nginx/logs/error.log

# 安装启动依赖项
RUN apk update \
    # 从编译阶段读取需要的库
    && runDeps="$( cat $NGINX_PATH/nginx.depends )" \
    #&& echo $runDeps \
    # 通过上面查找nginx运行需要的库
    && apk add --no-cache --virtual .nginx-rundeps $runDeps tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 开放80和1935端口
EXPOSE 80
EXPOSE 1935

# 使用这个指令允许用户自定义应用在收到 docker stop 所发送的信号,是通过重写 signal 库内的 stopsignal 来支持自定义信号的传递,在上层调用时则将用户自定义的信号传入底层函数
STOPSIGNAL SIGTERM

# 启动nginx命令
CMD ["/usr/local/nginx/sbin/nginx", "-g","daemon off;"]

创建镜像

docker build -t jcflv:v1 .

nginx.conf配置文件

user root;
worker_processes  1;



error_log logs/error.log error;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    server {
        listen       80;

        location / {
            root   /usr/local/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /live {
            flv_live on; #打开 HTTP 播放 FLV 直播流功能
            chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复

            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /tmp;
            add_header 'Cache-Control' 'no-cache';
        }

         location /play {
            flv;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' '*';
            add_header 'Access-Control-Allow-Headers' '*';
            alias live_record;
        }
        location /dash {
            root /tmp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /stat {
           rtmp_stat all;
           rtmp_stat_format json;
           access_log /usr/local/nginx/logs/user.log;
        }

        location /control {
            rtmp_control all; #rtmp 控制模块的配置
        }
    }
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;

rtmp {
    out_queue           4096;
    out_cork            8;
    max_streams         128;
    timeout             15s;
    drop_idle_publisher 15s;

    log_interval 10s; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
    log_size     1m; #log 模块用来记录日志的缓冲区大小

    server {
        listen 1935;


        application jucheng {
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间

            record all;
            record_path /usr/local/nginx/live_record;
            record_suffix .flv;
            record_append on;


            exec_record_done /usr/local/bin/yamdi -i $path -o /usr/local/nginx/video/$filename;
            exec_record_done sh  -c " echo $path >> /tmp/test.txt";

        }

        application hls {
            live on;

            hls on;
            hls_path /usr/local/nginx/html/hls;#视频流存放地址
            hls_fragment 5s;
            hls_playlist_length 15s;
            hls_continuous on; #连续模式。
            hls_cleanup on;    #对多余的切片进行删除。
            hls_nested on;     #嵌套模式。

            record all;
            record_path live_record;
            record_suffix .flv;
            record_append on;
        }

        application dash {
            live on;
            dash on;
            dash_path /tmp/dash;
        }
    }
}

使用方法

docker run -p80:80 -p1935:1935 -v 本地目录:/usr/local/nginx/live_record \
-v 本地目录:/usr/local/nginx/video --name nginx-flv -d jcflv:v1

你可能感兴趣的:(nginx,运维,服务器)