【问题标题】:nginx - Trying to secure a HLS stream through auth_requestnginx - 尝试通过 auth_request 保护 HLS 流
【发布时间】:2017-05-24 11:54:40
【问题描述】:

我花了很多时间寻找答案,相信我已经尝试了所有方法。 我正在运行一个将 rtmp 流推送到 HLS 流的 nginx 服务器。

下面是我的 nginx.conf 的一部分

location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
        }

        root /mnt/;
        set $auth_request_uri "http://SERVER:8000/auth_ext.php?token=$arg_token";
        auth_request /auth/;
        add_header Cache-Control no-cache; # Prevent caching of HLS fragments
        add_header Access-Control-Allow-Origin *; # Allow web player to access our playlist
    }

location /auth/ {
        internal;
        proxy_pass              $auth_request_uri;
        proxy_pass_request_body off;
        proxy_set_header        Content-Length "";
        proxy_set_header        X-Original-URI $request_uri;
    }

我正在尝试通过 php 页面对流进行身份验证,在该页面中我从 URL 获取参数,然后如果它与我的数据库中的令牌匹配,则响应 200 OK。

到目前为止,我成功地进行了身份验证,这意味着如果令牌匹配,我可以访问http://SERVER:8080/hls/stream.m3u8?token=TOKEN,但这就是发生的情况。

我有一个根据带宽调整流的主 m3u8 流,当我在控制台中访问 stream.m3u8 时,我看到了这个

 http://SERVER:8080/hls/stream.m3u8?token=TOKEN
 http://SERVER:8080/hls/stream_mid.m3u8
 http://SERVER:8080/hls/stream_hd720.m3u8
 http://SERVER:8080/hls/stream_src.m3u8

最后三个 m3u8 响应 404,因为参数不通过,因此我有一个永远不会加载但 URL 响应的流。 此外,在 m3u8 本身中,.ts 文件也得到 404。

如何处理这个问题,所以每当对第一个 m3u8 的第一次调用被验证时,剩余的 m3u8 和 ts 文件可以被访问或返回 200 代码?

我真的希望我说清楚了,我可以提供更多细节

谢谢大家

【问题讨论】:

    标签: php nginx http-live-streaming m3u8


    【解决方案1】:

    身份验证结果不会在请求之间共享。您必须为所有变体播放列表媒体片段的每个新请求提供相同的令牌。

    要实现这一点,您必须重写主 HLS 清单和变体 HLS 清单,并将令牌添加到每个包含的 URL。一种解决方案是使用 PHP 包装器拦截所有 .m3u8 请求,解析播放列表并即时添加令牌,然后返回具有正确 MIME 类型(application/x-mpegURLvnd.apple.mpegURL)的结果。

    最后一点,您应该使用签名令牌以避免重复使用。

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 2020-08-20
      • 2019-01-20
      • 1970-01-01
      • 2017-11-23
      • 2018-06-12
      • 2018-08-05
      • 2021-07-07
      • 2016-01-06
      相关资源
      最近更新 更多