共計 1505 個字符,預計需要花費 4 分鐘才能閱讀完成。
Nginx 如何搭建簡單直播服務器,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
前言
使用 Nginx + Nginx-rtmp-module 在 Ubuntu 中搭建簡單的 rtmp 推流直播服務器。
服務器環境
Ubuntu 16.04
相關概念
RTMP:RTMP 協議是 Real Time Message Protocol(實時信息傳輸協議) 的縮寫,它是由 Adobe 公司提出的一種應用層的協議。依賴于 flash 播發器來拉流。
請求樣式:rtmp://xxx.xxx.xxx.xxx:1935/ttest(命名空間)/test(推流碼)
HLS:蘋果出的一套 Http Live Streaming 協議,它的工作原理簡單來說就是把一段視頻流,分成一個個小的基于 HTTP 的文件來下載。通過讀取.m3u8 文件讀取一個一個的視頻流片段。
請求樣式:http://xxx.xxx.xxx.xxx/video(nginx 配置路由)/test.m3u8
nginx-rtmp-module:基于 Nginx 的流媒體 Server
測試工具
1. 推流工具:易推流 (ios)
2. 拉流工具:VLC media player(pc)
實現步驟
一、安裝或升級 Nginx
sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install nginx
二、安裝 nginx-rtmp-module
sudo apt-get install libnginx-mod-rtmp
三、編寫 nginx 配置
1. /etc/nginx/nginx.conf
user ubuntu; rtmp{ server{ listen 1935; chunk_size 4000; application test{ live on; record off; hls on; hls_path /usr/local/src/nginx/html/test; hls_fragment 1s; hls_playlist_length 3s; } } }
2. /etc/nginx/sites-enables/default
location /video/ { alias /usr/local/src/nginx/html/godeyeTest/; # 余下三行配置是解決跨域問題 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET, POST, OPTIONS add_header Access-Control-Allow-Headers DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization }
四、新建路徑,重啟 nginx 服務
1. mkdir 配置中的路徑 (/usr/local/src/nginx/html/test)
2. serive nginx restart
看完上述內容,你們掌握 Nginx 如何搭建簡單直播服務器的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注丸趣 TV 行業資訊頻道,感謝各位的閱讀!