GETTING STARTED WITH NGINX RTMP

By Thanh Tùng   Posted at  tháng 2 07, 2020   VPS No comments
Build Utilities
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev git zlib1g-dev -y
On Amazon Linux: sudo yum install git gcc make pcre-devel openssl-devel zlib1g-dev
Make and CD to build directory (home)
sudo mkdir ~/build && cd ~/build
Download & unpack latest nginx-rtmp (you can also use http)
sudo git clone git://github.com/arut/nginx-rtmp-module.git
Download & unpack nginx (you can also use svn)
sudo wget http://nginx.org/download/nginx-1.14.1.tar.gz
sudo tar xzf nginx-1.14.1.tar.gz
cd nginx-1.14.1
Build nginx with nginx-rtmp
sudo ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
sudo make
sudo make install
Start nginx Server
sudo /usr/local/nginx/sbin/nginx

SET UP LIVE STREAMING

To set up RTMP support you need to add rtmp{} section to nginx.conf (can be found in PREFIX/conf/nginx.conf). Stock nginx.conf contains only http{} section.
sudo nano /usr/local/nginx/conf/nginx.conf
Use this nginx.conf instead of stock config:
#user  nobody;
worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

        # sample handlers
        #location /on_play {
        #    if ($arg_pageUrl ~* localhost) {
        #        return 201;
        #    }
        #    return 202;
        #}
        #location /on_publish {
        #    return 201;
        #}

        #location /vod {
        #    alias /var/myvideos;
        #}

        # rtmp stat
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            # you can move stat.xsl to a different location
            root /usr/build/nginx-rtmp-module;
        }

        # rtmp control
        location /control {
            rtmp_control all;
        }

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

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

        application myapp {
            live on;

            # sample play/publish handlers
            #on_play http://localhost:8080/on_play;
            #on_publish http://localhost:8080/on_publish;

            # sample recorder
            #recorder rec1 {
            #    record all;
            #    record_interval 30s;
            #    record_path /tmp;
            #    record_unique on;
            #}

            # sample HLS
            #hls on;
            #hls_path /tmp/hls;
            #hls_sync 100ms;
        }

        # Video on demand
        #application vod {
        #    play /var/Videos;
        #}

        # Video on demand over HTTP
        #application vod_http {
        #    play http://localhost:8080/vod/;
        #}
    }
}
Restart nginx with:
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx

STATISTICS

Navigate your browser to http://localhost:8080/stat to see current streaming statistics, connected clients, bandwidth etc.

CONFIG RTMP HLS

push rtmp://localhost:1935/show
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000;

        application show {
            live on;
            # Turn on HLS
            hls on;
            hls_path /usr/local/nginx/html/hls;
            hls_fragment 3;
            hls_playlist_length 60;
            # disable consuming the stream from nginx as rtmp
            allow play all;

        }
    }
}

REPUSH SERVER ANOTHER

push rtmp://domain.com:1935/application/;

HTTP SERVER CONFIG

Since HLS consists of static files, a simple http server can be set up with two additions, correct MIME types and CORS headers.
server {
    listen 8080;

    location /hls {
        # Disable cache
        add_header Cache-Control no-cache;

        # CORS setup
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length';

        # allow CORS preflight requests
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }

        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /mnt/;
    }
}

TẠO TOKEN BẢO MẬT CHO NGINX

By Thanh Tùng   Posted at  tháng 2 07, 2020   PHP No comments
Code tạo token
<?PHP
$secret = ‘vietgithiviet’;
$id = $_GET[‘id’];
$ttl = 7200;
$ip = $_SERVER[‘REMOTE_ADDR’]; //nếu là local thì điền 127.0.0.1
$token = buildSecureLink($secret,$ttl,$ip);
function buildSecureLink($secret,$ttl,$ip){
$expires = time() + $ttl;
$md5 = md5(“$expires$ip $secret”, true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, ‘+/’, ‘-_’);
$md5 = str_replace(‘=’, ”, $md5);
return $md5.’/’.$expires;
};
$m3u = ‘http://localhost/live/’.$token.’/’.$id.’.m3u8′;
echo $m3u;
?>
Code bảo mật trong nginx
rewrite ^/live/(.*)/(.*)/(.*)$ /live/$3?token=$1&expires=$2 last;
secure_link $arg_token,$arg_expires;
secure_link_md5 “$secure_link_expires$remote_addr vietgithiviet”;
if ($secure_link = “”) { return 403; }
if ($secure_link = “0”) { return 410; }
Link xuất sẽ có dạng
http://localhost/live/’.$token.’/’.$id.’.m3u8

LẤY IP THẬT CỦA CLIENT QUA CLOUDFLARE

By Thanh Tùng   Posted at  tháng 2 07, 2020   PHP No comments
function getUserIP() {
if (isset($_SERVER[“HTTP_CF_CONNECTING_IP”])) {
$_SERVER[‘REMOTE_ADDR’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
$_SERVER[‘HTTP_CLIENT_IP’] = $_SERVER[“HTTP_CF_CONNECTING_IP”];
}
$client = @$_SERVER[‘HTTP_CLIENT_IP’];
$forward = @$_SERVER[‘HTTP_X_FORWARDED_FOR’];
$remote = $_SERVER[‘REMOTE_ADDR’];
if(filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; }
elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; }
else { $ip = $remote; }
return $ip;
}
echo getUserIP();
Connect with Us

Powered by Blogger.
back to top