rtmp流媒体 nginx服务端搭建

标签(空格分隔): 直播

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”:audio=”麦克风 (Realtek High Definition Audio)” -vcodec libx264 -acodec copy -preset:v ultrafast -tune:v zerolatency -f flv “rtmp://p855c26b6.live.126.net/live/685f6451704349a8bed4489f170d4084?wsSecret=1e0357b0ce7e101ac0613396148ae2b4&wsTime=1523430887”

netstat -ntlp 查看端口占用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

#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”:audio=”麦克风 (Realtek High Definition Audio)” -vcodec libx264 -acodec copy -preset:v ultrafast -tune:v 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

1
./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;     #嵌套模式。
    }
}

}