网站首页 > 技术教程 正文
目前很多静态资源,都可以无权限验证,进行访问或转发,对有价值的资源进行签权,限制转发无法在代码中实现拦截,我们可以使用nginx对视频、音频、图片等静态资源网址,加token签权
如:
http://192.168.1.22/123.mp3
http://192.168.1.22/123.m3u8
http://192.168.1.22/123.flv
对这些资源想增加token进行验证,如 :
http://192.168.1.22/123.flv?token=123
后端接口对token进行验证,通过即可以访问,不通过 跳转到其它 连接
1、下载nginx,这里是用window版本
由于需要用到lua脚本,所以下载第三方插件版本的
OpenResty https://openresty.org/en/
解压后修改配置文件conf/nginx.conf
在http 中增加以下配置
server {
listen 8018;
server_name localhost;
location /proxyprd {
#访问验证token接口 并提交传参
rewrite ^/180m7s/(.*) /$1 break;
proxy_pass http://125.7.23.10:8011/LuaVideoCheck/luaVideoCheck;
}
location /180m7s {
#访问地址域名:端口/180m7s
default_type text/plain;
access_by_lua '
local myIP = ngx.req.get_headers()["X-Real-IP"]
if myIP == nil then
myIP = ngx.req.get_headers()["x_forwarded_for"]
else
end
if myIP == nil then
myIP = ngx.var.remote_addr
end
local tokenstr= ""
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
if key == "token" then
tokenstr=val
end
end
local urlstr = ngx.var.uri
local pos = string.find (urlstr,".st")
local posseghik = string.find (urlstr,"seghik")
local posm3u8 = string.find (urlstr,".m3u8")
if pos and posseghik then
if not posm3u8 then
ngx.exec("@180m7sUrl")
return
end
end
local res = ngx.location.capture("/proxyprd", {args={token=tokenstr, ip=myIP,url=urlstr,sysname="proxyprd"}})
#如果接口luaVideoCheck验证token通过返回1,转发原始视频流内容
if res.body=="1" then
ngx.exec("@180m7sUrl")
return
end
if res.body=="-1" then
#如果接口luaVideoCheck验证token不通过返回-1,转发空内容
return
end
return
';
}
location @180m7sUrl{
#视频原始访问域名端口
#local urlstr = ngx.var.uri
#local urlstr=ngx.req.get_headers()["User-Agent"]
rewrite /180m7s/(.*) /$1 break;
proxy_pass http://59.5.36.80:6060;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
在你的接口http://xxx.xxx.xx...xx/LuaVideoCheck/luaVideoCheck 中添加验证程序
public int luaVideoCheck(string ip = "", string token = "", string url = "", string sysname = "")
{
if(token=="123")
{
return 1;
}
else
{
return -1;
}
}
保存配置
运行程序
测试步骤:
1、请求:http://nginx服务器的ip:8018/147m7s/123.flv?token=123
2、接口自动验证token:http://xxx.xxx.xx...xx/LuaVideoCheck/luaVideoCheck
3、验证通过,内容请求会自动转发到 http://59.5.36.80:6060/123.flv
猜你喜欢
- 2024-10-10 如何使用 Nginx 搭建一个具有缓存功能的反向代理服务呢?
- 2024-09-16 Windows使用nginx时,端口被占用怎么办?
- 2024-09-16 快速建立php的Windows开发环境(win10搭建php环境)
- 2024-09-16 windows下php开发环境php+nginx单步安装
- 2024-09-16 Windows+php7+nginx1.14+apache+mysql配置
- 2024-09-16 windows环境下tomcat+nginx负载均衡集群配置,动静分离
- 2024-09-16 在windows环境下 nginx + .net core 3.1 实现反向代理和负载均衡
- 2024-09-16 如何解决Nginx服务器,启动成功,访问无效
- 2024-09-16 超强windows10稳定Nginx绿色环境,可无限自定义PHP和mysql版本、同时运行N个版本
- 2024-09-16 windows服务器配置nginx日志分割(nginx 日志配置)
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)