nginx-rtmp-module推送权限控制

nginx-rtmp-module默认所有客户端都可以推流,那么问题就来了,怎么限制客户端推流呢?nginx-rtmp-module提供了限制发布者以及观看者的IP地址:

allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;

视乎IP地址限制,达不到需求。

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


nginx.conf

http {

    # nginx default configs...

    server {
        listen 8080;
        server_name localhost;

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/src/nginx-rtmp-module;
        }

        location /control {
            rtmp_control all;
        }

        error_page 500 502 503 504 /50x.html;
            location = /50.html {
            root html;
        }

        location ~ .php$ {
            root html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        application myapp {
            live on;

            on_publish http://localhost:8080/on_publish.php;
        }
}

on_publish.php


<?php

// ?user=user&pass=pass

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

if (empty($user) || empty($pass)) {
    echo "wrong query input";
    header('HTTP/1.0 404 Not Found');
    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();
}

?>

此代码简单的作了用户验证,也可以改成带数据库验证以及加入更多参数进行验证。

客服端推流设定 以Open Broadcaster Software(OBS)为例:

FMS URL:

rtmp://localhost:1935/myapp 播放路径/串码流(如果存在):

test?user=user&pass=pass

其中test为streamkey?user=user&pass=pass用来验证的

播放地址:

http://localhost:8080/test