Nginx Secure_link 防盗链:保护您的 M3U8 视频
以下是一个使用 PHP 和 Nginx 的 secure_link 防盗链代码示例,可有效保护您的 M3U8 视频资源,防止未经授权的访问。
Nginx 配置
在 Nginx 配置文件中添加以下内容:
location /videos/ {
valid_referers none blocked example.com;
if ($invalid_referer) {
return 403;
}
secure_link $arg_md5,$arg_expires;
secure_link_md5 '$secure_link_expires$uri$remote_addr secretkey';
if ($secure_link = '') {
return 403;
}
if ($secure_link = '0') {
return 410;
}
add_header Cache-Control 'no-cache';
add_header Content-Type 'application/vnd.apple.mpegurl';
alias /path/to/videos/;
}
PHP 代码
在 PHP 文件中生成链接:
<?php
$secret_key = 'secretkey';
$expires = time() + 3600; // 1 hour
$uri = '/videos/video.m3u8'; // the actual video file
$hash = md5('$expires$uri$_SERVER[REMOTE_ADDR]$secret_key');
$url = 'https://example.com/videos/video.m3u8?md5=$hash&expires=$expires';
echo $url;
?>
使用说明
- 将
example.com替换为您的实际域名。 - 将
$secret_key替换为您的密钥,确保密钥足够复杂且安全。 - 确保您的 Nginx 服务器已安装
secure_link模块。
注意: 为了防止缓存,代码中设置了 Cache-Control: no-cache 头部,确保视频每次都从服务器获取。
通过上述配置,您将能够有效地防止未经授权的用户访问您的 M3U8 视频文件。
原文地址: https://www.cveoy.top/t/topic/onHN 著作权归作者所有。请勿转载和采集!