实践: 1.1.1 使用nginx提供直播服务



0. 前言

想试试用nginx搞直播

1. 直播用的协议

现在一般用rtmp推流,用http-flv或者hls播放

2. nginx部分

set -e

yum install gcc gcc-c++ cmake -y

groupadd nginx
useradd -g nginx nginx

yum install pcre-devel openssl openssl-devel zlib-devel -y

git clone https://github.com/winshining/nginx-http-flv-module.git
# 下载nginx
# cd 到nginx源码目录
./configure --user=nginx --group=nginx \
  --prefix=/usr/local/nginx \
  --with-http_ssl_module \
  --with-pcre \
  --with-http_realip_module \
  --with-http_v2_module \
  --add-module=/usr/local/src/nginx-http-flv-module
make
make install

nginx.conf

# 要单核
worker_processes 1;

http {
  ...
  server {
    listen 80;

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

      root /data/nginx/stream;
      add_header 'Cache-Control' 'no-cache';
    }
  }
}

rtmp {
  server {
    listen 1985;
    chunk_size 4000;

    application myapp {
      live on;
      hls on;
      hls_path /data/nginx/stream/hls;
    }
  }
}

3. 推流部分

参考文件:去查rtmp和flv的github,那边说明很详细