這兩天為了利用云服務器實現 Nginx 進行OBS Rtmp推流,Flv拉流時發生了諸多情況,記錄實現過程。
環境
- OS:阿里云CentOS 7.9 64位
- Nginx:nginx-1.28.0
- Nginx-http-flv-module:nginx-http-flv-module-1.2.12
安裝Nginx編譯的依賴包:
yum install -y gcc gcc-c++ autoconf automake make
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel wget httpd-tools vim -y
Nginx配置
2.1 獲取nginx
源碼
wget http://nginx.org/download/nginx-1.28.0.tar.gz
2.2 解壓
tar -xzvf nginx-1.28.0.tar.gz
2.3 重命名
mv nginx-1.28.0 nginx
2.4 移動到/usr/local/
mv nginx/ usr/local/
Nginx-http-flv-module配置
3.1 在/usr/local/nginx
中下載
wget https://github.com/winshining/nginx-http-flv-module/archive/refs/tags/v1.2.12.tar.gz
3.2 解壓
tar -xzvf v1.2.12.tar.gz
3.3 關閉防火墻
systemctl disable --now firewalld
setenforce 0
getenforce
3.4 配置nginx編譯環境
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/nginx-http-flv-module-1.2.12
3.5編譯&安裝
make
make install
3.6設置Nginx環境變量
vi /etc/profile # vi 打開環境變量文件
export PATH=$PATH:/usr/local/nginx/sbin # 添加到文件最后一行
source /etc/profile # 立即生效
修改Nginx配置文件
4.1 將下方文本替換/usr/local/nginx/conf/nginx.conf
的內容:
#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_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;rtmp {out_queue 4096;out_cork 8;max_streams 128;timeout 15s;drop_idle_publisher 15s;log_interval 5s; #log模塊在access.log中記錄日志的間隔時間,對調試非常有用log_size 1m; #log模塊用來記錄日志的緩沖區大小server {listen 1935;#ffmpeg推流的application application app {live on;allow publish all;allow play all;record off;meta copy;gop_cache on; #打開GOP緩存,減少首屏等待時間}}
}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; # http-flv 拉流端口#server_name localhost;location /live {flv_live on; #打開HTTP播放FLV直播流功能chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回復add_header 'Access-Control-Allow-Origin' '*'; #添加額外的HTTP頭add_header 'Access-Control-Allow-Credentials' 'true'; #添加額外的HTTP頭}#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#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;# }#}}
4.2 新建/usr/local/nginx/logs
文件夾存放日志
sudo mkdir -p /usr/local/nginx/logs # sudo 是使用root權限創建
4.3 測試配置文件是否問題
/usr/local/nginx/sbin/nginx -t
4.4 運行Nginx
nginx
4.5 查看端口,如果1935 & 80
端口正在監聽就表明完成了
netstat -tulnp # 查看所有端口 加上 | grep x 可以過濾出端口x的情況
5.OBS推流
rtmp://${服務器公網地址}:1935/app/room-1
使用obs進行推流,其中推流地址和推流碼為:
6.使用VLC進行拉流
6.1 rtmp拉流地址為:rtmp://${服務器公網地址}:80/app/room-1
6.2 flv 拉流地址為:http://${服務器公網地址}:80/live?port=1935&app=app&stream=room-1
flv拉流地址,其中
live
的是配置文件中的http
模塊設置的、port
的值應該是推流端口、app
的值是配置文件中rtmp
模塊自定義的名稱、stream
的值是OBS推流自定義的推流碼。
效果:
( 延遲大概15s )
7. 參考
- 使用Nginx搭建流媒體服務器
- nginx+ffmpeg推流環境搭建
- CentOS7安裝 FFmpeg