Nginx 不内建流媒体功能,所有 RTMP、HLS、HTTP-FLV 等能力均依赖第三方模块(如 nginx-rtmp-module),必须源码编译并显式添加模块,且 rtmp{} 块须与 http{} 同级,不可嵌套;包管理器安装的 Nginx 默认不含相关模块,配置会报 “unknown directive 'rtmp'”。

必须从源码编译,不能用包管理器安装
apt/yum 安装的 nginx(如 sudo apt install nginx)默认不含任何流媒体模块,即使配置了 rtmp{} 块也会报错:unknown directive "rtmp"。你只能自己下载源码、加模块、重新编译。
关键步骤:configure 时指定模块路径
编译前必须运行 ./configure 并带上 --add-module= 参数:
- 模块路径必须是 源码根目录(即包含
config文件的那一层),例如:--add-module=/path/to/nginx-rtmp-module,不是/path/to/nginx-rtmp-module/src/ - 推荐用
git clone https://github.com/arut/nginx-rtmp-module.git获取,避免解压路径出错 - 若同时需要 HTTPS 和基础功能,加上常用选项:
--with-http_ssl_module --with-http_flv_module --with-http_mp4_module
配置文件里 rtmp 块必须和 http 平级
rtmp{} 不是 HTTP 的子配置,不能写在 http{} 里面。它要放在 nginx.conf 最外层,与 http、events 同级:
events {
worker_connections 1024;
}
http {
server { ... }
}
rtmp {
server {
listen 1935;
application live {
live on;
}
}
}
否则启动时直接失败,错误明确提示 unknown directive "rtmp"。
别漏掉编译依赖
缺少基础库会导致 ./configure 报错退出,常见提示如 “the HTTP rewrite module requires the PCRE library”。务必提前装全:
- Ubuntu/Debian:
sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev - CentOS/RHEL:
sudo yum install gcc pcre-devel openssl-devel zlib-devel


















