当前位置:首页 > 行业动态 > 正文

如何搭建 Nginx 流媒体服务器?

nginx流媒体服务器搭建需要安装 nginx和相关模块,配置nginx.conf文件以支持 流媒体传输协议,如hls或rtsp。

Nginx 流媒体服务器搭建

一、所需资源

1、Nginx:作为基础 HTTP 和反向代理服务器,用于提供静态文件服务和负载均衡,下载地址:[http://nginx.org/download/nginx-1.12.1.tar.gz](http://nginx.org/download/nginx-1.12.1.tar.gz)

2、OpenSSL:用于编译 Nginx 时支持 HTTPS,下载地址:[https://github.com/openssl/openssl/archive/OpenSSL_1_1_0f.tar.gz](https://github.com/openssl/openssl/archive/OpenSSL_1_1_0f.tar.gz)

3、nginx-rtmp-module:用于实现 RTMP 协议的模块,下载地址:[https://github.com/arut/nginx-rtmp-module/archive/v1.2.0.tar.gz](https://github.com/arut/nginx-rtmp-module/archive/v1.2.0.tar.gz)

4、FFmpeg:用于推流和拉流测试,安装命令:sudo apt-get install ffmpeg

二、搭建步骤

1、下载并解压软件包

 mkdir -p ~/nginx && cd ~/nginx
   wget http://nginx.org/download/nginx-1.12.1.tar.gz
   wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_0f.tar.gz
   wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.0.tar.gz
   tar -zxvf nginx-1.12.1.tar.gz
   tar -zxvf OpenSSL_1_1_0f.tar.gz
   tar -zxvf nginx-rtmp-module-1.2.0.tar.gz

2、编译 OpenSSL

 cd openssl-1.1.0f
   ./config --prefix=pwd/bin
   make && make install

3、编译 Nginx

在编译前需要确保安装了 PCRE 库和 zlib 库,使用以下命令进行安装:

 sudo apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev

然后执行以下命令编译 Nginx:

 cd nginx-1.12.1
   ./configure --prefix=pwd/bin --add-module=~/nginx/nginx-rtmp-module-1.2.0 --with-http_ssl_module
   make
   make install

4、配置 Nginx

nginx-rtmp-module/test/nginx.conf 复制到nginx-1.12.1/bin/conf 目录下,并进行相应修改:

 cp ~/nginx/nginx-rtmp-module-1.2.0/test/nginx.conf ~/nginx/nginx-1.12.1/bin/conf/
   vim ~/nginx/nginx-1.12.1/bin/conf/nginx.conf

修改后的配置文件如下:

 rtmp {
       server {
           listen 1935;
           application live {
               live on;
               # Turn on HLS
               hls on;
               hls_path /tmp/hls;
               hls_fragment 5s;
               hls_playlist_length 60s;
           }
       }
   }
   http {
       include       mime.types;
       default_type  application/octet-stream;
       sendfile        on;
       keepalive_timeout  65;
       server {
           listen       8080;
           server_name  localhost;
           location / {
               root   html;
               index  index.html index.htm;
           }
           location /hls {
               types {
                   application/vnd.apple.mpegurl m3u8;
                   video/mp2t ts;
               }
               alias /tmp;
               add_header Cache-Control no-cache;
           }
       }
   }

5、启动 Nginx

 ~/nginx/nginx-1.12.1/bin/sbin/nginx

6、验证安装

使用 FFmpeg 推流:

 ffmpeg -re -i input.mp4 -c copy -f flv rtmp://localhost:1935/live/stream

使用 VLC 播放:

打开 VLC,选择“媒体”->“打开网络串流”,输入以下地址:

 rtmp://localhost:1935/live/stream

或者通过浏览器访问 HLS 流:

 http://localhost:8080/hls/live/stream.m3u8

三、常见问题及解答(FAQs)

问题1:为什么无法找到 OpenSSL 库?

答:在编译 Nginx 之前,确保已正确安装并指定 OpenSSL 路径,可以通过修改configure 脚本中的路径来解决问题。

./configure --with-http_ssl_module --with-openssl=/usr/local/openssl

问题2:如何更改 RTMP 端口号?

答:可以在 Nginx 配置文件中的listen 指令后修改端口号,将listen 1935; 改为listen 2000;,然后重新加载 Nginx 配置:

sudo /etc/init.d/nginx reload
0