556 字
2 分钟

rtmp流媒体 nginx服务端搭建

2018-07-10
2019-08-07

标签(空格分隔): 直播


mkdir nginx

切换到nginx目录,下载压缩包并解压到当前工作目录:

wget http://nginx.org/download/nginx-1.12.1.tar.gz

tar -zxvf nginx-1.12.1.tar.gz

安装依赖的的库:

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

下载rtmp模块,生成目录nginx-rtmp-module:

git clone https://github.com/arut/nginx-rtmp-module.git

切换到nginx-1.12.1目录,执行(如果想build调试版本的话,要添加–with-debug):

./configure –add-module=/home/sunrayme/nginx/nginx-rtmp-module make

sudo make install

运行nginx:

sudo /usr/local/nginx/sbin/nginx

ffmpeg -f dshow -i video=”BisonCam, NB Pro”=”麦克风 (Realtek High Definition Audio)” -vcodec libx264 -acodec copy -preset ultrafast -tune zerolatency -f flv “rtmp://p855c26b6.live.126.net/live/685f6451704349a8bed4489f170d4084?wsSecret=1e0357b0ce7e101ac0613396148ae2b4&wsTime=1523430887”

netstat -ntlp 查看端口占用

#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application myapp {
live on;
}
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 3s;
}
}
}
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 8080;
#charset koi8-r;
#access_log logs/host.access.log main;
location /state {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /vagrant_date/nginx-rtmp-module-master;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#加入hls支持
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /tmp/hls; #视频流文件目录(自己创建)
expires -1;
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;
# }
#}
}

配置文件验证

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

ffmpeg -f dshow -i video=”BisonCam, NB Pro”=”麦克风 (Realtek High Definition Audio)” -vcodec libx264 -acodec copy -preset ultrafast -tune zerolatency -f flv “rtmp://192.168.1.113/live live=1”

rtmp://192.168.1.113:1935/hls/test rtmp://192.168.1.113:1935/myapp/test hls

加上mp4flv播放和清缓存

./configure –with-http_ssl_module –add-module=../nginx-rtmp-module –with-http_v2_module –with-http_ssl_module –with-http_sub_module –with-http_stub_status_module –with-http_gzip_static_module –with-pcre

./configure --prefix=/usr/local/nginx/ --add-module=../nginx-rtmp-module --add-module=../ngx_cache_purge-master --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_mp4_module --with-http_flv_module

yum -y install pcre-devel openssl openssl-devel

有时候,我们需要单独安装nginx,来处理大量的下载请求。单独在Centos5安装nginx遇到的rewrite和HTTP cache错误解决办法:

wget http://nginx.org/download/nginx-0.8.33.tar.gz tar -zxvf nginx-0.8.33.tar.gz cd nginx-0.8.33 ./configure –prefix=/usr/local/nginx

安装Nginx时报错

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题 yum -y install pcre-devel

错误提示:./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using –without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using –with-http_ssl_module –with-openssl=options.

解决办法:

yum -y install openssl openssl-devel

总结:

yum -y install pcre-devel openssl openssl-devel

./configure –prefix=/usr/local/nginx

make

make install

昌书电脑安装记录

nginx path prefix: “/usr/local/nginx/“ nginx binary file: “/usr/local/nginx//sbin/nginx” nginx modules path: “/usr/local/nginx//modules” nginx configuration prefix: “/usr/local/nginx//conf” nginx configuration file: “/usr/local/nginx//conf/nginx.conf” nginx pid file: “/usr/local/nginx//logs/nginx.pid” nginx error log file: “/usr/local/nginx//logs/error.log” nginx http access log file: “/usr/local/nginx//logs/access.log” nginx http client request body temporary files: “client_body_temp” nginx http proxy temporary files: “proxy_temp” nginx http fastcgi temporary files: “fastcgi_temp” nginx http uwsgi temporary files: “uwsgi_temp” nginx http scgi temporary files: “scgi_temp”

编译时出现 set but not used [-Werror=unused-but-set-variable] 问题的解决办法: 找到对应目录(nginx中)中的Makefile文件,找到 -Werror 字段,去掉-Werror,重新编译,则问题解决

rtmp://p855c26b6.live.126.net/live/685f6451704349a8bed4489f170d4084?wsSecret=aec4eda8a73a12c2dd525319608bf533&wsTime=1523520333
rtmp://192.168.1.119:1935/myapp/test
rtmp://192.168.1.119:1935/myapp/test
rtmp://192.168.1.119:1935/hls/test
http://111.231.138.185:81/hls/test
#user nobody;

worker_processes 2;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events { worker_connections 1024; }

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 8080;
#charset koi8-r;
#access_log logs/host.access.log main;
location /state {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /vagrant_date/nginx-rtmp-module-master;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#加入hls支持
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /tmp/hls; #视频流文件目录(自己创建)
expires -1;
add_header Cache-Control no-cache;
}
}

}

rtmp_auto_push on; #因为Nginx可能开启多个子进程,这个选项表示推流时,媒体流会发布到多个子进程

rtmp {

server {
listen 1935; #监听的端口
chunk_size 4000;
application myapp {
live on;
}
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 3s; #每个视频切片的时长
hls_playlist_length 0s; #总共可以回看的事件,这里设置的是1分钟。
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
}
}

}

rtmp流媒体 nginx服务端搭建
https://www.motouguai.com/mo-ren-fen-lei/rtmp-streaming-nginx-server-build/
作者
Murry
发布于
2018-07-10
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时