# nginx-rtmp-live **Repository Path**: ukav/nginx-rtmp-live ## Basic Information - **Project Name**: nginx-rtmp-live - **Description**: 一个基于 Nginx + RTMP 模块构建的专业级视频直播解决方案,支持多种协议和高性能流媒体传输。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-21 - **Last Updated**: 2025-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UKAV Nginx RTMP 直播系统 一个基于 Nginx + RTMP 模块构建的专业级视频直播解决方案,支持多种协议和高性能流媒体传输。 ![预览](images/01.gif) ## 🎯 项目特性 ### 核心功能 - **多协议支持**: RTMP推流、HLS播放、DASH流媒体 - **实时录制**: 自动录制直播内容,支持多种格式 - **实时监控**: 完整的监控dashboard和统计信息 - **高性能**: 基于Nginx的高并发处理能力 - **易部署**: Docker容器化,一键部署 - **可扩展**: 模块化设计,支持自定义扩展 ### 技术架构 - **Nginx**: 高性能Web服务器 + RTMP模块 - **Docker**: 容器化部署 - **Redis**: 缓存和会话管理 - **MySQL**: 数据存储 - **Prometheus + Grafana**: 监控和可视化 - **Bootstrap + Video.js**: 现代化Web界面 ## 🚀 快速开始 ### 系统要求 - Docker 20.0+ - Docker Compose 2.0+ - 4GB+ RAM - 10GB+ 磁盘空间 ### 一键安装 ```bash # 克隆项目 git clone https://gitee.com/ukav/nginx-rtmp-live.git cd nginx-rtmp-live # 赋予执行权限 chmod +x scripts/start.sh # 初始化并启动服务 ./scripts/start.sh init ``` ### 手动安装 ```bash # 1. 创建必要目录 mkdir -p ssl monitoring sql # 2. 生成SSL证书 openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout ssl/key.pem -out ssl/cert.pem \ -subj "/C=CN/ST=Beijing/L=Beijing/O=UKAV/CN=localhost" # 3. 启动服务 docker-compose up -d --build # 4. 检查服务状态 docker-compose ps ``` ## 📺 使用说明 ### 访问地址 - **主页**: http://localhost - **播放器**: http://localhost/player.html - **统计页面**: http://localhost/stat - **监控面板**: http://localhost:3000 (admin/admin123) ### 推流配置 #### OBS Studio 配置 1. 打开 OBS Studio 2. 设置 -> 推流 3. 服务:自定义 4. 服务器:`rtmp://localhost:1935/live` 5. 推流密钥:`your_stream_key` #### FFmpeg 推流 ```bash # 推流本地视频文件 ffmpeg -re -i video.mp4 -c copy -f flv rtmp://localhost:1935/live/your_stream_key # 推流摄像头 ffmpeg -f v4l2 -i /dev/video0 -c:v libx264 -preset ultrafast -tune zerolatency -c:a aac -f flv rtmp://localhost:1935/live/your_stream_key # 推流桌面录制 ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast -f flv rtmp://localhost:1935/live/your_stream_key ``` ### 播放地址 - **HLS**: http://localhost/hls/your_stream_key.m3u8 - **DASH**: http://localhost/dash/your_stream_key.mpd - **RTMP**: rtmp://localhost:1935/live/your_stream_key ### 测试流 - 海洋:http://vjs.zencdn.net/v/oceans.mp4 - 西瓜视频 Demo:https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4 - 大兔子:http://www.w3school.com.cn/example/html5/mov_bbb.mp4 - 大灰熊:https://www.w3schools.com/html/movie.mp4 - 冰川:https://media.w3.org/2010/05/sintel/trailer.mp4 - 新闻视频1:https://stream7.iqilu.com/10339/upload_transcode/202002/09/20200209105011F0zPoYzHry.mp4 - 新闻视频2:https://stream7.iqilu.com/10339/upload_transcode/202002/09/20200209104902N3v5Vpxuvb.mp4 - 西瓜播放器 Demo:https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8 - 大白兔:https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8 ` ## 🔧 配置说明 ### 主要配置文件 #### nginx.conf Nginx主配置文件,包含: - RTMP模块配置 - HTTP服务配置 - HLS/DASH设置 - 录制和截图功能 - SSL配置 #### docker-compose.yml Docker服务编排文件,定义: - Nginx RTMP服务 - Redis缓存服务 - MySQL数据库 - 监控服务(Prometheus + Grafana) ### 环境变量 ```bash # 时区设置 TZ=Asia/Shanghai # 数据库配置 MYSQL_ROOT_PASSWORD=ukav123456 MYSQL_DATABASE=rtmp_live MYSQL_USER=rtmp_user MYSQL_PASSWORD=rtmp_pass # Grafana配置 GF_SECURITY_ADMIN_PASSWORD=admin123 ``` ### 端口说明 - **80**: HTTP Web服务 - **443**: HTTPS服务 - **1935**: RTMP推流端口 - **3306**: MySQL数据库 - **6379**: Redis缓存 - **3000**: Grafana监控 - **9090**: Prometheus ## 📊 监控功能 ### 实时统计 - 在线流数量 - 观看人数统计 - 带宽使用情况 - 服务器状态监控 ### Grafana Dashboard 访问 http://localhost:3000 查看详细监控信息: - 流媒体统计 - 服务器性能 - 网络流量 - 错误日志分析 ### 命令行监控 ```bash # 查看服务状态 docker-compose ps # 查看实时日志 docker-compose logs -f nginx-rtmp # 查看统计信息 curl http://localhost/stat ``` ## 🛠️ 高级配置 ### 自定义流密钥 编辑 `nginx.conf` 中的 RTMP 配置,添加认证逻辑: ```nginx application live { live on; # 添加推流认证 on_publish http://localhost:8080/auth; } ``` ### 录制设置 ```nginx # 录制所有流 record all; record_path /var/recordings; record_unique on; record_suffix .flv; # 录制特定流 record video; record_path /var/recordings/video; ``` ### HLS 配置优化 ```nginx # HLS设置 hls on; hls_path /var/www/html/hls; hls_fragment 2; # 分片大小(秒) hls_playlist_length 10; # 播放列表长度 hls_continuous on; # 连续模式 ``` ### SSL/HTTPS 配置 #### 启用HTTPS(推荐用于生产环境) ```bash # 方式1:一键启用自签名证书 ./scripts/enable-https.sh auto # 方式2:交互式配置 ./scripts/enable-https.sh ``` #### 使用正式SSL证书 1. 准备SSL证书文件 2. 将证书放在 `ssl/` 目录: ``` ssl/cert.pem # 证书文件 ssl/key.pem # 私钥文件 ``` 3. 启用HTTPS配置: ```bash ./scripts/enable-https.sh # 选择 "3. 仅启用HTTPS配置" ``` #### Let's Encrypt证书 ```bash ./scripts/enable-https.sh # 选择 "2. 配置Let's Encrypt证书" # 按提示操作 ``` ## 🔍 故障排除 ### 常见问题 #### 0. SSL配置错误 ```bash # 如果遇到SSL证书相关错误: # "no ssl_certificate is defined for the listen ... ssl" # 解决方案1:禁用HTTPS ./scripts/enable-https.sh # 选择 "5. 禁用HTTPS" # 解决方案2:启用HTTPS ./scripts/enable-https.sh auto ``` #### 1. 推流连接失败 ```bash # 检查防火墙 sudo ufw allow 1935 # 检查服务状态 docker-compose logs nginx-rtmp # 测试端口连通性 telnet localhost 1935 ``` #### 2. 播放卡顿或无法播放 ```bash # 检查HLS文件生成 ls -la /var/www/html/hls/ # 检查nginx错误日志 docker-compose logs nginx-rtmp | grep error # 检查浏览器控制台错误 ``` #### 3. 服务无法启动 ```bash # 检查端口占用 netstat -tlnp | grep :80 netstat -tlnp | grep :1935 # 清理Docker缓存 docker system prune -f # 重新构建镜像 docker-compose up --build --force-recreate ``` ### 性能优化 #### Nginx 优化 ```nginx worker_processes auto; worker_connections 4096; # 启用gzip压缩 gzip on; gzip_types text/plain application/json application/x-javascript text/css; # 优化缓冲区 client_body_buffer_size 128k; client_max_body_size 10m; ``` #### 系统优化 ```bash # 调整文件描述符限制 ulimit -n 65536 # 调整内核参数 echo 'net.core.somaxconn = 65536' >> /etc/sysctl.conf echo 'net.ipv4.tcp_max_syn_backlog = 65536' >> /etc/sysctl.conf sysctl -p ``` ## 📝 开发指南 ### 项目结构 ``` ukav_nginx/ ├── nginx.conf # Nginx主配置 ├── docker-compose.yml # Docker编排 ├── Dockerfile # 镜像构建 ├── html/ # Web页面 │ ├── index.html # 主页 │ ├── player.html # 播放器 │ └── stat.xsl # 统计样式 ├── scripts/ # 脚本文件 │ └── start.sh # 启动脚本 ├── ssl/ # SSL证书 ├── monitoring/ # 监控配置 ├── sql/ # 数据库脚本 └── README.md # 项目文档 ``` ### 自定义开发 1. 修改HTML模板定制界面 2. 添加API接口扩展功能 3. 集成第三方服务(CDN、云存储等) 4. 开发移动端APP ### API接口 ```javascript // 获取统计信息 GET /stat // 获取流列表 GET /api/streams // 控制流状态 POST /api/streams/{stream_key}/control // 获取录制文件列表 GET /api/recordings ``` ## 📄 许可证 本项目基于 MIT 许可证开源,详见 [LICENSE](LICENSE) 文件。 ## 🤝 贡献指南 欢迎提交 Pull Request 和 Issue! 1. Fork 项目 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送分支 (`git push origin feature/AmazingFeature`) 5. 开启 Pull Request ## 📞 技术支持 - **项目地址**: https://gitee.com/ukav/nginx-rtmp-live - **问题反馈**: https://gitee.com/ukav/nginx-rtmp-live/issues - **技术文档**: https://nginx.org/en/docs/ - **RTMP规范**: https://www.adobe.com/devnet/rtmp.html ## 🙏 致谢 感谢以下开源项目: - [Nginx](https://nginx.org/) - [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) - [Video.js](https://videojs.com/) - [Bootstrap](https://getbootstrap.com/) - [Docker](https://www.docker.com/) --- **UKAV Team** © 2024 - 专业的流媒体解决方案