寫在前面
最近幾年,直播行業比較火,無論是傳統行業的直播,還是購物、游戲、教育,都在涉及直播。作為在互聯網行業奮斗了多年的小伙伴,你有沒有想過如果使用Nginx搭建一套直播環境,那我們該如何搭建呢?別急,接下來,我們就一起使用Nginx來搭建一套直播環境。
安裝Nginx
注意:這里以CentOS 6.8服務器為例,以root用戶身份來安裝Nginx。
1.安裝依賴環境
1
2
3
4
5
6
7
8
|
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* autoconf automake zlib* fiex* libxml* libmcrypt* libtool-ltdl-devel* libaio libaio-devel bzr libtool |
2.安裝openssl
1
2
3
4
5
6
|
wget https: //www .openssl.org /source/openssl-1 .0.2s. tar .gz tar -zxvf openssl-1.0.2s. tar .gz cd /usr/local/src/openssl-1 .0.2s . /config --prefix= /usr/local/openssl-1 .0.2s make make install |
3.安裝pcre
1
2
3
4
5
6
|
wget https: //ftp .pcre.org /pub/pcre/pcre-8 .43. tar .gz tar -zxvf pcre-8.43. tar .gz cd /usr/local/src/pcre-8 .43 . /configure --prefix= /usr/local/pcre-8 .43 make make install |
4.安裝zlib
1
2
3
4
5
6
|
wget https: //sourceforge .net /projects/libpng/files/zlib/1 .2.11 /zlib-1 .2.11. tar .gz tar -zxvf zlib-1.2.11. tar .gz cd /usr/local/src/zlib-1 .2.11 . /configure --prefix= /usr/local/zlib-1 .2.11 make make |
5.下載nginx-rtmp-module
nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module
使用命令:
1
|
git clone https: //github .com /arut/nginx-rtmp-module .git |
6.安裝Nginx
1
2
3
4
5
6
|
wget http: //nginx .org /download/nginx-1 .19.1. tar .gz tar -zxvf nginx-1.19.1. tar .gz cd /usr/local/src/nginx-1 .19.1 . /configure --prefix= /usr/local/nginx-1 .19.1 --with-openssl= /usr/local/src/openssl-1 .0.2s --with-pcre= /usr/local/src/pcre-8 .43 --with-zlib= /usr/local/src/zlib-1 .2.11 --add-module= /usr/local/src/nginx-rtmp-module --with-http_ssl_module make make install |
這里需要注意的是:安裝Nginx時,指定的是openssl、pcre和zlib的源碼解壓目錄,安裝完成后Nginx配置文件的完整路徑為:/usr/local/nginx-1.19.1/conf/nginx.conf。
配置Nginx
配置Nginx主要是對Nginx的nginx.conf文件進行配置,我們可以在命令行輸入如下命令編輯nginx.conf文件。
1
|
vim /usr/local/nginx-1 .19.1 /conf/nginx .conf |
在文件中添加如下內容。
1
2
3
4
5
6
7
8
9
10
11
12
|
rtmp { server { listen 1935; #監聽的端口 chunk_size 4096; application hls { #rtmp推流請求路徑 live on; hls on; hls_path /usr/share/nginx/html/hls ; hls_fragment 5s; } } } |
其中,hls_path需要可讀可寫的權限。接下來,我們創建/usr/share/nginx/html/hls 目錄。
1
2
|
mkdir -p /usr/share/nginx/html/hls chmod -R 777 /usr/share/nginx/html/hls |
接下來,修改http中的server模塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/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; } } |
然后啟動Nginx:
1
|
/usr/local/nginx-1 .19.1 /sbin/nginx -c /usr/local/nginx-1 .19.1 /conf/nginx .conf |
使OBS推流
OBS(Open Broadcaster Software) 是以互聯網流媒體直播內容為目的免費和開放源碼軟件。需要下載這個軟件,借助這個軟件進行推流(電腦沒有攝像頭的貌似安裝不了。。。)
OBS的下載鏈接為:https://obsproject.com/zh-cn/download。
安裝后,桌面上會有一個如下所示的圖表。
打開后我們需要有一個場景,并且在這個場景下有一個流的來源(可以是窗口,如果選的是視頻則會自動識別攝像頭),接下來就是設置了。
在配置中最需要關注的就是流的配置,由于是自建的流媒體服務器所以我們按照如下所示的方式進行配置。
1
|
rtmp: // 你的服務器ip:端口(1935) /live #URL填寫流的地址 |
設置完成我們就可以 開始推流了。
拉流測試地址
推薦一個拉流的測試地址,里面針對各種協議都能測試拉流測試,需要注意圖中幾個地方,由于我們使用的rtmp協議,我們選擇這一欄,底下填寫我們推流的地址和我們在上面obs的設置里面配置的流的名稱,start, ok搞定!!!
到此這篇關于使用Nginx搭建流媒體服務器實現直播功能的文章就介紹到這了,更多相關nginx 搭建流媒體服務器實現直播內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/binghe001/p/13363105.html