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 搭建点播和直播流媒体服务器的完整步骤与方法。