Centos7 搭建Nginx+rtmp+hls直播推流服務器


1 准備工具

使用yum安裝git

[root~]# yum -y install git

下載nginx-rtmp-module,官方github地址

// 通過git clone 的方式下載到服務器上
[root~]# git clone https://github.com/arut/nginx-rtmp-module.git

yum 安裝 openssl

[root~]# yum -y install openssl openssl-devel

 

2 安裝Nginx服務器,官方網址

下載Nginx解壓,並添加rtmp和openssl支持

如果需要nginx監控模塊 下面倒數第二行后面添加 

--with-http_stub_status_module 
//這里我安裝的是 nginx-1.10.3 版本
[root~]# wget http://nginx.org/download/nginx-1.10.3.tar.gz 
[root~]# tar -zxvf nginx-1.10.3.tar.gz 
[root~]# cd nginx-1.10.3
//添加rtmp和openssl支持
[root~]# ./configure --add-module=/替換為自己的安裝路徑(path/to)/nginx-rtmp-module --with-http_ssl_module
[root~]# make && make install

如果已經安裝過Nginx,只需要找到Nginx源碼目錄添加rtmp的支持即可

1.查看當前安裝的Nginx版本
[root~]# /usr/local/nginx/sbin/nginx -v
查詢結果:nginx version: nginx/1.10.3
2.再使用find命令查找其位置
[root~]# find / -name nginx-1.10.3
查詢結果:/root/nginx-1.10.3
3.cd到查詢到的源目錄
[root~]# cd  /root/nginx-1.10.3
4.添加rtmp的支持(如果看到一個綠色的 configure 文件就說明查找對了)
[root~]# ./configure --add-module=/替換為自己的安裝路徑(path/to)/nginx-rtmp-module
[root~]#  make && make install
5.啟動nignx
[root~]# /usr/local/nginx/sbin/nginx 

 

3 修改Nginx配置文件nginx.conf

使用vi命令打開 nginx.conf,輸入 i 進入編輯狀態

[root~]# vi /usr/local/nginx/conf/nginx.conf  
# 在http節點同級加上rtmp配置:
rtmp {
    server {
        listen 1935;
        chunk_size 2048;
drop_idle_publisher 10s; #指定的時間內丟棄空閑的publisher application rtmplive { live on; record off; notify_method get;
# 跳轉的時候,默認以POST方式傳輸參數,修改成GET方式(權限驗證url參數方式用) on_publish http://127.0.0.1/on_publish; # 推流權限驗證地址(可選)

       

        #轉推流
        push rtmp://****;

          }

     # HLS 配置(可選)
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls; #需要手動創建路徑
            hls_continuous on; #切片編號從上一次結束開始
        }
    }
}

#http配置 簡單權限驗證 HLS配置
http {

    server {

        listen      8080;
        
        # 推流權限驗證
        location /on_publish {
            default_type text/html;
            if ( $args ~ pass=1234 ) { # 如果url參數包括 pass=1234
                return 200;
            }
            return 404;
        }

        # This URL provides RTMP statistics in XML
        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/;
        }

        # HLS(可選)
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp; #root path
            add_header Cache-Control no-cache;
        }
    }
}

 

 nginx狀態監控頁面(需前面安裝http_stub_status_module)

        # nginx狀態監控頁面
        location /status {
            # Turn on nginx stats
            stub_status on;
            # I do not need logs for stats
            access_log   off;
            # Security: Only allow access from 192.168.1.100 IP #
            #allow 192.168.1.100;
            # Send rest of the world to /dev/null #
            #deny all
        }

 

重啟nginx

[root~]# nginx -s reload #方法一
[root~]# systemctl restart nginx.service #方法二

 

開機啟動

# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx 
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
# systemctl enable nginx.service
systemctl start nginx.service    啟動nginx
systemctl stop nginx.service    結束nginx
systemctl restart nginx.service    重啟nginx

 

4 推流

使用obs推流

url填寫 rtmp://[服務器ip]:[端口]/[nginx配置中rtmp的application名稱],如 rtmp://10.129.0.100:1935/hls

如配置HLS,需填寫串流碼,名稱隨意,如 demo,這個名稱在HLS方式拉流時會用到(筆者測試中發現,個別電腦推流填寫串流碼名稱后,使用vlc不能直接播放rtmp,去掉串流碼名稱后可正常播放)

如配置url驗證,在url后面添加驗證參數,如 rtmp://10.129.0.100:1935/hls?pass=1234,“?pass=1234”可以放在串碼流中

 

 

 

5 拉流

使用vlc拉流

填寫網絡URL 如 http://10.129.0.100:8080/hls/demo.m3u8

注意m3u8的文件名與推流時填的串流碼一致

或者填寫rtmp鏈接,如 rtmp://10.129.0.100:1935/hls

 

 

附:

nginx默認路徑:/usr/local/nginx/html

 


在Centos7上搭建Nginx+rtmp服務器

centos7 搭建rtmp+hls直播流服務器及rtmp轉hls直播流(nginx+nginx-rtmp-module-master+obs)

基於Nginx搭建RTMP/HLS視頻直播流媒體服務器

搭建rtmp推流服務器

nginx-rtmp-module(github)

Setup Nginx-RTMP on CentOS 7

nginx nginx-rtmp-module 基於rtmp協議實現公網推流拉流

centos7安裝nginx-rtmp

Nginx RTMP配置文件種HLS部分

【官方文檔】Nginx模塊Nginx-Rtmp-Module學習筆記(二)HLS 指令詳解

nginx-rtmp加入權限驗證的簡單方法

nginx-rtmp加入權限驗證的簡單方法

Nginx配置中的if判斷

「自己開發直播」實現nginx-rtmp-module多頻道輸入輸出與權限控制

在實戰中使用nginx-rtmp遇到的TCP連接問題分析

Nginx 服務器性能Bug和性能優化方案(真實經歷)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM