简体   繁体   English

如何在使用 https 的 React 应用程序中调用 hls 服务器

[英]How to call an hls server in a react app that is using https

So, I have a react app that is built and run through a nodejs server.因此,我有一个通过 nodejs 服务器构建和运行的 React 应用程序。 This nodejs server is run with nginx which handles the cert.此 nodejs 服务器与处理证书的 nginx 一起运行。 This react/nodejs server also is running a RTMP server this RTMP server is running an hls server.这个 react/nodejs 服务器也在运行一个 RTMP 服务器这个 RTMP 服务器正在运行一个 hls 服务器。

So, basically the website has certs and runs over https://.所以,基本上该网站有证书并运行在 https:// 上。 I need to know what approach to take to call the hls server to display a stream on the website.我需要知道采取什么方法来调用 hls 服务器以在网站上显示 stream。 The problem is that the package I am using "node-media-server" doesn't make it obvious how to convert the http to https and so when I cam calling the url from the react app I get a "(blocked:mixed-content)" error.问题是我正在使用“节点媒体服务器”的 package 没有明确说明如何将 http 转换为 https,因此当我从 React 应用程序调用 url 时,我得到一个“(阻塞:混合-内容)”错误。 when i am calling the.m3u8 file.当我调用 the.m3u8 文件时。 So the react app will call the url "http://website.com/live/stream/index.m3u8".所以 React 应用程序将调用 url“http://website.com/live/stream/index.m3u8”。 Do I need to convert this hls stream to https?我需要将此 hls stream 转换为 https 吗? If so how do I do this?如果是这样,我该怎么做? maybe I need to change my nginx config?也许我需要更改我的 nginx 配置?

Below is my nginx configuration.下面是我的 nginx 配置。

upstream socketio {
#       ip_hash;
        server 127.0.0.1:8174;
}

upstream reactserve {
        server 127.0.0.1:3000;
}


upstream hls {
        server 127.0.0.1:8179;
}

#server {
#       listen          80;
#        server_name     www.idealgambler.com;
#       rewrite         ^(.*)   https://$host$1 permanent;


#}

server {

        listen                  443 ssl;
        ssl                     on;
        server_name             idealgambler.com www.idealgambler.com;

        access_log              /var/log/nginx/access-ssl.log;
        error_log               /var/log/nginx/error-ssl.log;


        location / {
        #       include proxy_params;
                proxy_pass              http://socketio;
                proxy_next_upstream     error timeout invalid_header http_500 http_502 http_503 http_504;
                proxy_redirect          off;
                proxy_buffering         off;

                proxy_set_header        Host                    $host;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        X-Forwarded-Proto       $scheme;
                proxy_set_header Access-Control-Allow-Origin *;
                add_header              Front-End-Https         on;
                add_header              Access-Control-Allow-Origin *;
        }

        location /socket.io/ {
                proxy_pass              http://socketio;
                proxy_redirect off;

                proxy_http_version      1.1;

                proxy_set_header        Upgrade                 $http_upgrade;
                proxy_set_header        Connection              "upgrade";

                proxy_set_header        Host                    $host;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header Access-Control-Allow-Origin *;
                add_header              Access-Control-Allow-Origin *;
        }

        location /hls {
                proxy_pass              http://hls;
                proxy_redirect off;
                proxy_set_header        Host                    $host;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header Access-Control-Allow-Origin *;
                add_header              Access-Control-Allow-Origin *;
        }


    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


    ssl_certificate /etc/letsencrypt/live/idealgambler.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/idealgambler.com-0001/privkey.pem; # managed by Certbot

}

Here is the NodeMediaServer code in nodejs:这是nodejs中的NodeMediaServer代码:

require("./Casino/CasinoRouters/client-connected")(app, io);

const NodeMediaServer = require("node-media-server");
const config = {
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 60,
    ping_timeout: 30,
  },
  http: {
    port: 8179,
    mediaroot: "./media",
    allow_origin: "*",
  },
  log_file: "./logs/nms.log",
  trans: {
    ffmpeg: process.env.FFMPEG_PATH,
    tasks: [
      {
        app: "live",
        hls: true,
        hlsFlags: "[hls_time=2:hls_list_size=3:hls_flags=delete_segments]",
        hlsKeep: true, // to prevent hls file delete after end the stream
        dash: true,
        dashFlags: "[f=dash:window_size=3:extra_window_size=5]",
        dashKeep: true, // to prevent dash file delete after end the stream
      },
    ],
  },
};

var nms = new NodeMediaServer(config);
nms.run();

So I have tried to update the nginx file, as well as changing the config for the nodemediaserver to https. The problem with changing http to https in the config is that the key "mediaroot" does not exist in https of nodemediaserver.所以我尝试更新 nginx 文件,并将 nodemediaserver 的配置更改为 https。在配置中将 http 更改为 https 的问题是节点媒体服务器的 https 中不存在密钥“mediaroot”。 The error I get is that it can't find the file "./media" when changing it to https in the express app.我得到的错误是在 express 应用程序中将其更改为 https 时找不到文件“./media”。 So basically the https hls will not run only http will run.所以基本上 https hls 不会运行,只有 http 会运行。 perhaps there is a different config for running it through https.也许有一个不同的配置可以通过 https 运行它。

I have tried adding the hls to the nginx configuration, but I don't think this did anything.我尝试将 hls 添加到 nginx 配置,但我认为这没有任何作用。

Ideally you should try to fix the URL and make it match the scheme of application (ie never mix http:// with https://) or make the app detect and correct it if the URL comes from an external source.理想情况下,您应该尝试修复 URL 并使其与应用程序方案相匹配(即永远不要将 http:// 与 https:// 混合使用),或者如果 URL 来自外部来源,则让应用程序检测并更正它。

If it's not doable you might try to fall back to inserting Content-Security-Policy: upgrade-insecure-requests in the HTTP response headers.如果它不可行,您可能会尝试回退到在 HTTP 响应标头中插入Content-Security-Policy: upgrade-insecure-requests This would make the browser automatically convert all http:// to https:// as if the latter was originally requested by the app.这将使浏览器自动将所有 http:// 转换为 https://,就好像后者最初是由应用程序请求的一样。 You seem to have control over the deployment so this can be accomplished within Nginx configuration.您似乎可以控制部署,因此这可以在 Nginx 配置中完成。 Alternatively the header can be added to HTML head, as shown in this answer .或者,可以将 header 添加到 HTML 头部,如本答案所示。

Watch out for WebSockets too.还要注意 WebSockets。 Secure ones (wss://) may require Sec-WebSocket-* headers to be passed by the proxy as per RFC6455 .安全的 (wss://) 可能需要根据RFC6455由代理传递Sec-WebSocket-*标头。 Nginx doesn't do it by default. Nginx 默认不这样做。 You will probably want to add the following lines to the configuration:您可能希望将以下行添加到配置中:

location /socket.io/ {
    # ...

    # pass client-to-server Sec-WebSocket-* headers
    proxy_set_header Sec-WebSocket-Key        $http_sec_websocket_key;
    proxy_set_header Sec-WebSocket-Version    $http_sec_websocket_version;
    proxy_set_header Sec-WebSocket-Extensions $http_sec_websocket_extensions;
    proxy_set_header Sec-WebSocket-Protocol   $http_sec_websocket_protocol;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM