openresty +rtmp 搭建視訊直播伺服器
最近搭建了一個rtmp直播伺服器, 因為nginx的rtmp伺服器本身需要一些http的控制, 所以選用了openresty這個nginx+lua的http框架, 再其中包含rtmp伺服器, 這樣既可以充當rtmp直播伺服器,也可以充當http,還可以直接在其中用lua指令碼完成相關控制!
現具體說明:
系統環境:linux centos6.5
環境附屬安裝:
yum install gcc
yum install pcre-devel
yum install openssl-devel
yum install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev
yum install libxml2 libxml2-devel libxslt libxslt-devel
yum install perl perl-devel perl-ExtUtils-Embed
openresty 下載地址: https://openresty.org/en/download.html
裡面有widnows, linux各種版本, 選取好自己所用的版本, 本部落格用的是linux最新的版本
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
tar xzvf openresty-1.13.6.2.tar.gz
cd openresty-1.13.6.2/bundle/LuaJIT-2.1-20180420
make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
以上是單獨安裝luajit,這是以前的版本安裝方法, 現在最新的版本安裝luajit已經合併到./configure裡面自動編譯, 以上的步驟可以跳過, 不過我喜歡按這個方法來, 如果出現問題可以很好的知道哪出現問題,純屬個人喜好。
在budlle目錄下執行
wget
tar xzvf 2.3.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar xzvf v0.3.0.tar.gz
nginx rtmp module 下載最新的版本 地址: https://github.com/arut/nginx-rtmp-module
解壓到該目錄
到現在,準備工作已經完成, 現可以編譯
cd openresty-1.13.6.2
./configure –prefix=/opt/openresty –with-luajit –with-http_ssl_module –user=root –group=root –with-http_realip_module –add-module=./bundle/ngx_cache_purge-2.3/ –add-module=./bundle/nginx_upstream_check_module-0.3.0/ –add-module=./bundle/nginx-rtmp-module-master –with-cc-opt=”-I/usr/local/ssl/include” –with-ld-opt=”-L/usr/local/ssl/lib”
說明 加粗的這段是因為執行./configure時, 一直找不到本機的openssl庫, 所以手動新增該庫的地址, 所包含的引數具體都是幹什麼用的, 請自行百度谷歌。
make && make install
就安裝完成了,
現貼出所使用的conf配置, 一些常用的功能都已經新增, 在需要別的功能的時候, 我會更新本部落格
=================nginx.conf=====================================================
user root;
worker_processes 1;
daemon off;
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 {
#設定rtmp的日誌記錄, 該記錄只有在直播流結束後過顯示在日誌裡, 不能connect的階段記錄, 該問題暫未解決
log_format rtmp_main ‘
time_local]
app” “
args” ’
‘
bytes_sent “
flashver” (
connection $session_time ‘;
access_log logs/rtmp_access.log rtmp_main;
server{
listen 1935;
ack_window 5000;#視窗大小
chunk_size 1024;
application live {
live on;
wait_key on;
drop_idle_publisher 10s;
wait_video on;
interleave on;
sync 10ms;
}
}
}
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;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;#將stat.xsl檔案放置到安裝目錄裡的html資料夾中
}
location /stat.xsl {
root html;
}
location /nclients {
proxy_pass http://127.0.0.1/stat; #將nclients.xsl檔案放置到安裝目錄裡的html資料夾中, nclients.xsl具體內容在下面
xslt_stylesheet html/nclients.xsl app='$arg_app' name='$arg_name';
add_header Refresh "3; $request_uri";
}
location /test{
content_by_lua_file conf/lua/test.lua;#測試lua執行的helloworld
}
location /control{
rtmp_control all;#rtmp的http控制api生效
}
}
}
————-nclients.xsl———————————————————————–
以上就可以啟動nginx, 生效我們自己的rtmp伺服器!
如果覺得有用, 請關注我的部落格!!!!
做專注最接地氣流媒體相關內容!!!!
我以後也會盡可能,儘自己最大水平持續更新!!!!