簡體   English   中英

如何在 nginx hls 中使用身份驗證

[英]how to use authentication in nginx hls

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

rtmp {
    server {
        listen 8099;
        application live {
            live on;
            hls on;
            hls_path /data/live/hls;
            hls_playlist_length 4s;
            hls_fragment 1s;
            on_publish http://127.0.0.1/rtmp/publish;
            on_play http://127.0.0.1/rtmp/join;
            on_publish_done http://127.0.0.1/rtmp/close;
            on_play_done http://127.0.0.1/rtmp/leave;
        }
    }
}

http {
    server {
        listen  9000;
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /data/live;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*';
        }
    }
}

當我使用rtmp觀看視頻時,nginx可以回調到on_play( http://127.0.0.1/rtmp/join )。 當我離開時,nginx 可以回調到 on_play_done。

但是如何使用 hls 和回調到 on_play 和 on_play_done。

你不能在 hls 中使用on_playon_play_done因為一旦你轉換到 HLS 你攝取的視頻是純 HTML,所以這樣做的解決方案是使用ngx_http_auth_request_module ,考慮到這一點你的代碼應該看起來有點像這樣:

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

rtmp {
    server {
        listen 8099;
        application live {
            live on;
            hls on;
            hls_path /data/live/hls;
            hls_playlist_length 4s;
            hls_fragment 1s;
            on_publish http://127.0.0.1/rtmp/publish;
            on_play http://127.0.0.1/rtmp/join;
            on_publish_done http://127.0.0.1/rtmp/close;
            on_play_done http://127.0.0.1/rtmp/leave;
         }
    }
}

http {
    server {
        listen  9000;
        location /hls {
            auth_request /auth;
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /data/live;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*';
        }
        location = /auth {
            internal;
            proxy_pass http://auth-server; # -- replace with your auth server uri
            proxy_pass_request_body off;
            proxy_set_header        Content-Length "";
            proxy_set_header        X-Original-URI $request_uri;
        }
    }
}

如果向auth-server的子請求返回2xx響應碼,則允許訪問,如果返回 401 或 403,則拒絕訪問。 on_play基本相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM