如何使用nginx播放HLS流?

如何使用nginx播放HLS流?,nginx,rtmp,http-live-streaming,Nginx,Rtmp,Http Live Streaming,我有带rtmp模块的hginx。rtmp流工作正常,现在我想尝试hls流。 我的配置: rtmp { server { listen 1935; application myapp { live on; exec_pull /usr/bin/ffmpeg -i http://url-to-remote-webcam -map 0 -codec:v copy rtmp://my-ip:1935/myapp/m

我有带rtmp模块的hginx。rtmp流工作正常,现在我想尝试hls流。
我的配置:

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
            exec_pull /usr/bin/ffmpeg -i  http://url-to-remote-webcam
-map 0 -codec:v copy rtmp://my-ip:1935/myapp/mystream.flv  2>>/tmp/ffmpeg-$name.log;
        }

        application myapp {
           live on;
           hls on;
           hls_path /tmp/hls;
           hls_fragment 5s;
        }
    }
 }

 http {
      server {
           listen 8080;
           location /hls {
                 root /tmp;
           }
      }
 }
尝试在vlc播放器中打开url,但出现无法打开源代码的错误

我能错过什么

更新

我试图根据miknik的回答编辑配置

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
            exec_push ffmpeg -i http://url-to-remote-webcam -acodec copy -vcodec libx264 -vprofile baseline -g 10 -r 15 -s 640x360 -f flv rtmp://my-ip:1935/hls/mystream;
         }

         application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s;
          }
     }
  }

  http {
       server {
             listen 8080;
             location /hls {
                  types {
                        application/vnd.apple.mpegurl m3u8;
                  }
                  root /tmp;
                  add_header Cache-Control no-cache;
                  add_header Access-Control-Allow-Origin *;
            }
        }
    }
并尝试打开
http://my-ip:8080/hls/mystream.m3u8
但仍然出现相同的错误

这里是nginx日志中的错误:

2018/09/20 15:50:29 [error] 11406#11406: *2 open() "/tmp/hls/mystream.m3u8" failed (2: No such file or directory), client: client-ip, server: , request: "GET /hls/mystream.m3u8 HTTP/1.0", host: "server-ip:8080"

首先,两个rtmp应用程序都被称为
myapp
。调用第二个
hls

在myapp块中,您需要(可选地转换和)将流推送到hls块。所以你需要这样的指令:

exec_push ffmpeg -i rtmp://127.0.0.1:1935/stream/$name -acodec copy -vcodec libx264 -vprofile baseline -g 10 -r 15 -s 640x360 -f flv rtmp://127.0.0.1:1935/hls/$name;
location / {
  try_files $uri $uri/ =404;
}

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

location /stat.xsl {
  root /var/www/test/stat;
}

location /hls {
  types {
    application/vnd.apple.mpegurl m3u8;
    video/mp2t ts;
  }
  root /tmp;
  add_header Cache-Control no-cache;
  index index.m3u8 index.ts;
  add_header Access-Control-Allow-Origin *;
}
然后在http块中,您希望位置块如下所示:

exec_push ffmpeg -i rtmp://127.0.0.1:1935/stream/$name -acodec copy -vcodec libx264 -vprofile baseline -g 10 -r 15 -s 640x360 -f flv rtmp://127.0.0.1:1935/hls/$name;
location / {
  try_files $uri $uri/ =404;
}

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

location /stat.xsl {
  root /var/www/test/stat;
}

location /hls {
  types {
    application/vnd.apple.mpegurl m3u8;
    video/mp2t ts;
  }
  root /tmp;
  add_header Cache-Control no-cache;
  index index.m3u8 index.ts;
  add_header Access-Control-Allow-Origin *;
}

包括统计位置是值得的,因为它可以非常有用地找出某些东西没有按预期工作的原因。

检查nginx error.log以查看是否有任何线索。它可能会说“没有这样的文件或目录”,这表明您的位置块指向了错误的路径(只是一个示例)。@iangetz yeah in error.log我有此错误,但我已经设置了此文件夹的权限
chmod-R 755/tmp/hls
我试图编辑配置,但仍然不理解
rtmp
块。我在我的问题上添加了新的配置。