当前位置: 首页 > 编程文章 >

CentOS 7 使用 Nginx 搭建点播与直播流媒体服务器

CentOS 7 使用 Nginx 搭建点播与直播流媒体服务器

作者:叶言时间:2025-05-29 16:07:08浏览:706

本文介绍在 CentOS 7 环境下,通过编译安装 Nginx 并加载 nginx_mod_h264_streaming、nginx-rtmp-module 及 ngx_cache_purge 模块,构建支持 FLV/MP4 点播及 RTMP/HLS 直播的流媒体服务器。配置完成后,可通过访问特定 URL 路径直接播放视频内容。

本文目录展开

CentOS 7 下 Nginx 流媒体服务器搭建流程

运行环境基于 CentOS 7 操作系统,核心服务软件为 Nginx。

第一步,安装 Nginx 所需的依赖包。执行命令:yum install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel yamdi。

第二步,下载并解压 nginx_mod_h264_streaming 模块,以使 Nginx 支持 FLV 和 MP4 格式的流媒体播放。下载地址为 http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz。解压后,需修改 src 目录下的 ngx_http_streaming_module.c 文件,将包含 r->zero_in_uri 的 if 语句进行注释处理。

第三步,下载并解压 nginx-rtmp-module 模块,以赋予 Nginx 对 RTMP 和 HLS 协议的支持。使用命令 wget -o nginx-rtmp-module.zip https://github.com/arut/nginx-rtmp-module/archive/master.zip 获取源码。

第四步,下载用于清除缓存的模块。使用命令 wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip 获取 ngx_cache_purge 模块。

第五步,下载 Nginx 主程序源码包。下载地址为 http://nginx.org/download/nginx-1.9.0.tar.gz。

第六步,进入 Nginx 解压后的安装目录,执行 configure 命令进行配置。参数包括:--prefix=/usr/local/nginx/ 指定安装路径;--add-module=../nginx-rtmp-module-master 添加 RTMP 模块;--add-module=../ngx_cache_purge-master 添加缓存清理模块;--add-module=../nginx_mod_h264_streaming-2.2.7 添加 H264 流媒体模块;以及 --with-http_stub_status_module、--with-http_ssl_module、--with-http_sub_module、--with-http_gzip_static_module、--with-http_flv_module 启用相应 HTTP 功能模块。

第七步,执行 make && make install 命令编译并安装 Nginx。安装完成后,需修改安装目录下 objs 文件夹中的 Makefile 文件,删除其中的 -Werror 选项,以避免编译过程中的严格错误检查导致失败。

第九步,修改 Nginx 的主配置文件 nginx.conf,以适配流媒体服务需求。

第十步,通过 yum 停止并卸载 firewalld 防火墙,随后安装 iptables-services 服务。修改 /etc/sysconfig/iptables 配置文件,确保放行 80 端口,以便外部访问。

第十一步,具体的 Nginx 配置内容如下所示。


#使用的用户和组
#user nobody;
#指定工作衍生的进程数,为cpu的核心数总和
worker_processes 2;
#指定错误日志的存放路径 日志记录级别[debug,info,notice,warn,error,crit]
error_log  /usr/local/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定pid存放的路径
pid    /usr/local/nginx/logs/nginx.pid;
events {
  #使用的网络I/O模型,linux系统推荐是epoll而freeBSD是kqueue
  use epoll;
  #允许的连接数,最大的高并发连接数为worker_processes*worker_connections
  worker_connections 51200;
}

rtmp {
 server {
  listen 1935;
  chunk_size 4096;
  application live {
   live on;
   record off;
  }
  #application live2 {
   #live on;
   #record off;
  #}
  # video on demand
  application media {
   play /usr/local/nginx/html/;
  }
  #application vod_http {
   #play http://192.168.31.185/vod;
  #}
  application hls {
   live on;
   hls on;
   hls_path /tmp/hls;
  }
 }
}

http {
  include    mime.types;
  default_type application/octet-stream;
  #
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  gzip on;
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root  html;
      index index.html index.htm;
    }

location ~ \.flv$ {
  root /usr/local/nginx/html/media/;  
  flv;
}
location ~ \.mp4$ {
  root /usr/local/nginx/html/media/; 
  mp4;
}

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;
 }

    #error_page 404       /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }


    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #  proxy_pass  http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #  root      html;
    #  fastcgi_pass  127.0.0.1:9000;
    #  fastcgi_index index.php;
    #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    #  include    fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #  deny all;
    #}
  }

  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}

  # HTTPS server
  #
  #server {
  #  listen    443 ssl;
  #  server_name localhost;

  #  ssl_certificate   cert.pem;
  #  ssl_certificate_key cert.key;

  #  ssl_session_cache  shared:SSL:1m;
  #  ssl_session_timeout 5m;

  #  ssl_ciphers HIGH:!aNULL:!MD5;
  #  ssl_prefer_server_ciphers on;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
}

第十二步,配置生效后,在浏览器或播放器中输入 xxx.xxx.xxx.xxx/*.mp4 或 *.flv 即可正常播放视频文件。

以上即为使用 Nginx 搭建点播和直播流媒体服务器的完整步骤与方法。

上一篇
ThinkPHP实现分页显示功能

本文介绍在ThinkPHP框架中实现数据列表分页显示的具体方法。通过IndexController.class.php中的M模型查询与\Think\Page类配置,结合index.html模板中的表格渲染及{$show}变量输出,配合自定义CSS样式完成前端展示。

下一篇
Nginx配置htpasswd实现网站密码保护

本文介绍如何在Nginx服务器中通过htpasswd文件实现基于HTTP Basic认证的网站访问控制。配置过程包括修改server块添加auth_basic指令、生成加密密码并写入htpasswd文件,以及设置正确的文件权限。完成配置后重载Nginx即可生效,失败时返回401错误。

相关文章

更多
CentOS 7 编译安装 PHP 7.4 与 Nginx 1.17.6 完整指南

本文介绍在 CentOS 7 系统中通过源码编译方式安装 PHP 7.4.0 和 Nginx 1.17.6 的详细步骤。内容涵盖依赖包安装、源码解压配置、参数调整及配置文件修改,重点包括 php.ini 的上传大小与时区设置,以及 php-fpm.conf 的 PID 路径和日志配置,最后提供 Nginx 反向代理 PHP 的配置示例及服务启动测试方法。

2025.10.22 932
CentOS 7.2 基于 Nginx 与 vsftpd 搭建图片服务器指南

本文介绍在 CentOS 7.2 环境下,通过安装配置 Nginx 和 vsftpd 来构建图片服务器的完整流程。主要涵盖 Nginx 源码编译安装、进程管理、配置文件编写以及 FTP 目录映射,最终实现通过 HTTP 访问存储在 FTP 服务中的图片资源。

2025.11.21 920
CentOS 7 安装 Nginx 整合 Lua 环境教程

本文介绍在 CentOS 7 虚拟机环境中,通过源码编译方式安装 Nginx 并整合 Lua 运行环境的完整流程。主要涵盖网络配置、依赖库安装、LuaJIT 部署、Nginx 编译参数设置以及解决 libluajit-5.1.so.2 缺失报错等关键步骤,最终实现 HTTP 请求下的 Lua 脚本执行验证。

2025.06.08 845
CentOS 7 将 Nginx 添加为系统服务并设置开机自启

在 CentOS 7 中,编译安装 Nginx 后默认未注册为系统服务。本文介绍如何通过创建 /usr/lib/systemd/system/nginx.service 文件,利用 systemctl 命令管理 Nginx 状态、启动服务,并配置开机自动启动,实现服务的标准化运维。

2026.02.22 698
Metricbeat 6.2.4 监控 Nginx 部署指南

本文介绍在 CentOS 7.2 环境下,使用 Metricbeat 6.2.4 版本对 Nginx 1.13.9 进行实时监控的完整流程。内容涵盖从 RPM 包下载、安装到配置文件修改及服务启动的具体步骤。需确保 Nginx 编译时包含 http_stub_status_module 模块,并在 nginx.conf 中开启 basic_status 接口,以便 Metricbeat 采集 stubstatus 指标数据。

2025.10.19 658
CentOS 7 源码编译安装 Nginx 1.13.5 完整指南

本文介绍在 CentOS 7.3 64位系统上通过源码编译安装 Nginx 1.13.5 的完整流程。步骤涵盖安装依赖包、创建专用运行用户、下载 Nginx 与 OpenSSL 源码、配置编译参数并执行 make install,以及设置 systemd 开机自启和 Firewalld 防火墙规则,确保服务安全稳定运行。

2025.05.23 520

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。