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。
发布于 2022-05-28 00:27:31
您不能在hls中使用on_play和on_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一样。
https://stackoverflow.com/questions/71674335
复制相似问题