1. 程式人生 > >Nginx做流媒體服務Windows版實現直播

Nginx做流媒體服務Windows版實現直播

宣告:網上有很多關於Linux系統的配置,這裡就不記錄了。剛開始搭建直播室,用的是Java語言,

Java開源的流媒體服務有Red5,但是很久沒更新了,而且文件較少不容易上手,對於直播功能相對要求較低的還是選擇ngin來搭建流媒體服務,下面是記錄搭建步驟。

一、下載搭建時需要用到的軟體

1、下載 nginx 1.7.11.3 Gryphon

連結: http://nginx-win.ecsds.eu/download/nginx 1.7.11.3 Gryphon.zip

下載完成後解壓,將檔案重新命名因為含有空格,nginx

2、下載伺服器狀態檢查程式

連結:https://github.com/arut/nginx-rtmp-module/

下載完成後解壓,將解壓過的檔案放到nginx根目錄,例如:E:\nginx\nginx-rtmp-module

3、下載OBS推流軟體

連結:https://obsproject.com/ 進入官網選擇下載

二、配置流媒體服務

1、修改nginx伺服器中 conf 目錄下的 nginx-win.conf 檔案,配置如下

 

#user  nobody;
# multiple workers works !
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs
/error.log info; #pid logs/nginx.pid; events { worker_connections 8192; # max value 32768, nginx recycling connections+registry optimization = # this.value * 20 = max concurrent connections currently tested with one worker # C1000K should be possible depending there is enough ram/cpu power # multi_accept on; } rtmp { server { listen
1935; chunk_size 4000; notify_method get; application live { #rtmp推流地址:rtmp://ip地址:1935/live live on; on_publish http://192.168.8.210/on_publish.php; #rtmp推流鑑權,如果鑑權不通過則沒有許可權進行推流 } } } http { #include /nginx/conf/naxsi_core.rules; include mime.types; default_type application/octet-stream; #log_format main '$remote_addr:$remote_port - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; # # loadbalancing PHP # upstream myLoadBalancer { # server 127.0.0.1:9001 weight=1 fail_timeout=5; # server 127.0.0.1:9002 weight=1 fail_timeout=5; # server 127.0.0.1:9003 weight=1 fail_timeout=5; # server 127.0.0.1:9004 weight=1 fail_timeout=5; # server 127.0.0.1:9005 weight=1 fail_timeout=5; # server 127.0.0.1:9006 weight=1 fail_timeout=5; # server 127.0.0.1:9007 weight=1 fail_timeout=5; # server 127.0.0.1:9008 weight=1 fail_timeout=5; # server 127.0.0.1:9009 weight=1 fail_timeout=5; # server 127.0.0.1:9010 weight=1 fail_timeout=5; # least_conn; # } sendfile off; #tcp_nopush on; server_names_hash_bucket_size 128; ## Start: Timeouts ## client_body_timeout 10; client_header_timeout 10; keepalive_timeout 30; send_timeout 10; keepalive_requests 10; ## End: Timeouts ## #gzip on; server { listen 8081; server_name localhost; location /stat {             rtmp_stat all;             rtmp_stat_stylesheet stat.xsl;         }         location /stat.xsl {             root nginx-rtmp-module/;         }         location /control {             rtmp_control all;         } #charset koi8-r; #access_log logs/host.access.log main; ## Caching Static Files, put before first location #location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { # expires 14d; # add_header Vary Accept-Encoding; #} # For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode location / { #include /nginx/conf/mysite.rules; # see also http block naxsi include line ##SecRulesEnabled; ##DeniedUrl "/RequestDenied"; ##CheckRule "$SQL >= 8" BLOCK; ##CheckRule "$RFI >= 8" BLOCK; ##CheckRule "$TRAVERSAL >= 4" BLOCK; ##CheckRule "$XSS >= 8" BLOCK; root html; index index.html index.htm; } # For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi ##location /RequestDenied { ## return 412; ##} ## Lua examples ! # location /robots.txt { # rewrite_by_lua ' # if ngx.var.http_host ~= "localhost" then # return ngx.exec("/robots_disallow.txt"); # end # '; # } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # single backend process # fastcgi_pass myLoadBalancer; # or multiple, see example above # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl spdy; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_prefer_server_ciphers On; # ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM; # location / { # root html; # index index.html index.htm; # } #} }

 

推流鑑權檔案PHP:on_publish.php,可以為任意服務端檔案, on_publish 指定伺服器鑑權地址,此程式碼簡單的作了使用者驗證,也可以改成帶資料庫驗證以及加入更多引數進行驗證。
<?php

// ?user=user&pass=pass

$user = isset($_POST['user']) ? $_POST['user'] : '';
$pass = isset($_POST['pass']) ? $_POST['pass'] : '';

if (empty($user) || empty($pass)) {
    echo "wrong query input";
    header('HTTP/1.0 500 error...');
    exit();
}

$saveuser = $user;
$savepass = $pass;

if (strcmp($user, $saveuser) == 0 && strcmp($pass, $savepass) == 0) {
    echo "Username and Password OK";
} else {
    echo "Username or Password wrong";
    header('HTTP/1.0 404 Not Found');
    exit();
}

?>

注意:在接受引數時一定用POST接收,否則鑑權失敗。

更新配置檔案,執行cmd命令:

> cd nginx
> nginx.exe -c conf\nginx-win.conf

2、開啟OBS客戶端進行推流測試,按照如下步驟進行配置

注意:rtmp地址:rtmp://ip地址:1935/live?user=1&pass=2,後面的引數為鑑權所用,否則連線失敗。