• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

nginx + lua 资源防盗链

php 搞代码 3年前 (2022-01-23) 45次浏览 已收录 0个评论

使用说明

1. nginx安装lua扩展

2. 修改配置nginx配置文件

  1. set $salt “cmstopSalt”; md5的密钥串
  2. set $expire “2”; 过期时间
  3. set $timeType ‘seconds’; 过期时间类型 seconds为秒 期它值为分钟

3. 以下代码保存为lua文件

–获取md5值

function getMd5(time)

return ngx.md5(ngx.var.salt .. time)

end

–获取参数

local args = nil

if “GET” == ngx.var.request_method then

args = ngx.req.get_uri_args()

end

local key = args[‘key’]

local time = tonumber(args[‘time’])

–获取允许的时间范围

%本文来源gaodai#ma#com搞*代#码9网#搞gaodaima代码local ur = 0;

local interval = 1;

if ngx.var.timeType ~= ‘seconds’ then

interval = 60;

end

–验证key是否合法 是否过期

if time ~= nil and key ~= nil and string.len(key) == 32 and getMd5(time) == key and time + tonumber(ngx.var.expire) * interval >= tonumber(os.time()) then

ur = 1;

end

if 0 == ur then

ngx.exit(403)

end

4. 项目的nginx配置文件中增加以下代码

location ~ \.m3u8$ {

rewrite_by_lua_file ‘lua文件地址’;

}

5. PHP生成密钥规则与参数设置

//nginx中定义的值 如:cmstopSalt

index.m3u8?key=<?php echo md5(‘cmstopSalt’.$time)?>&time=<?php echo $time;?>

以上就介绍了 nginx + lua 资源防盗链,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:nginx + lua 资源防盗链

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址