教你如何搭建自己的直播伺服器-簡易

CoolDog;發表於2021-11-30

    在專案中有沒有遇見過要對接直播介面的需求?我想大家都是有的。但是怎麼說呢,對接第三方的缺點也很明顯,除去那不可避免的一些事故。最大的缺點就是要錢!!!要錢!!!要錢!!!

對於我們公司來說。一般都是要先上線,在考慮效率。所以經過我不斷地查詢資料找到了一款可以搭建直播伺服器的工具 。就是他 nginx.exe 

    下面直接開擼!首先放一下官網地址:http://nginx.org/en/download.html

    博主用的是window系統其它系統。。請繞路。下載安裝之後需要增加幾個資料夾。

 

 然後開啟\conf\下的nginx.conf  按照下面進行配置

worker_processes  1;   #Nginx程式數,建議設定為等於CPU總核數
 
events {
    worker_connections  1024;  #工作模式與連線數上限
}
 
rtmp_auto_push on;
 
 
#RTMP服務
rtmp{
    server{
        listen 1935;        #服務埠
        chunk_size 4096;    #資料傳輸塊的大小
        
        application vod{
            play ./vod;   #視訊檔案存放位置
        }
        application live{
            live on;                     #開啟直播
            hls on;                      #開啟hls直播。這個引數把直播伺服器改造成實時回放伺服器
            #wait_key on;                #對視訊切片進行保護,這樣就不會產生馬賽克了
            hls_path ./m3u8File;         #切片視訊檔案存放位置(HLS,m3u8檔案存放位置)
            hls_fragment 2s;             #每個視訊切片的時長
            hls_playlist_length 16s;
            recorder myRecord{
                record all manual;
                record_suffix _.flv;
                record_path ./rec;
            }
            #hls_continuous on;          #連續模式
            #hls_cleanup on;             #對多餘的切片進行刪除
            #hls_nested on;              #巢狀模式
        }
    }
}

 
 
#HTTP服務
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
 
    server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        location /live_hls{
            types{
                #m3u8 type設定
                application/vnd.apple.mpegurl m3u8;
                #ts分片檔案設定
                video/mp2t ts;
            }
            #指向訪問m3u8檔案目錄
            alias ./m3u8File;
                add_header Cache-Control no-cache; #禁止快取
                add_header Access-Control-Allow-Origin *; #允許所有域名跨域訪問代理地址
                add_header Access-Control-Allow-Headers X-Requested-With;
                add_header Access-Control-Allow-Methods GET; #跨域請求訪問請求方式,
        }
 
        location /control{
            rtmp_control all;
        }
        
        location /stat{
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl{
            root ./nginx-rtmp-module-master;
        }
 

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

然後我們進行執行直播伺服器,怎麼執行呢。直接執行.exe是無法進行完全啟動的。

我們開啟cmd命令;

 

 回車之後我們就可以開啟工作管理員看到程式是已經開啟了。

 

然後開啟遊覽器位址列中輸入localhost:80,出現下面圖片內容代表沒有問題。

 

 

 下一步我們進行obs推流 推流後可以讓其他埠進行拉流播放;

 

 拉流的話這裡提供兩種方式

第一種直接原地址rtmp:進行拉流。

另外一種是推薦的http:進行拉流 格式如下:http://localhost:80/live_hls/1441970998831222784.m3u8

 

可以看到訪問直播流是沒有問題的~ 

 

 

然後資料夾中也出現了我們推流的一些m3u8.檔案。

 

 

 

 

非常感謝看到這裡,那就在將我收藏的一些nginx.exe的命令給大家吧。

nginx命令:
nginx.exe -t   檢查配置是否正確
start nginx     啟動
nginx -s stop  立即停止服務
systemctl restart nginx.service   重啟Nginx服務
netstat -tlnp   檢視埠號

相關文章