Nginx:基于 LUA Redis 封锁内容的 URL(.MP4 或 .PNG)

我想要阻止用户访问某些内容(如视频和图像),如果用户没有在会话中。为了检查它是否经过身份验证,我在后端上使用它(通过 redis):hset 用户令牌123 on

示例 URL 请求:./content/s04-40dm4-de2020.mp4?token=token123

我想通过 lua-resty-redis 模块检查“token123”是否为“on”,如果 nginx 不会“阻止”请求,则允许正常访问。我不知道如何在不损失很多性能的情况下做到这一点,我目前正在将 redis 模块与此混合使用:

content_by_lua '
    ...(在 lua-resty-redis 中检查是否等于“on”)
     local file =“/ path ...”
     local f = io.open(file,“rb”)
     local content = f:read(“* all”)
     f:close()
     ngx.print(content)
'

是否有一种只有在未经过身份验证时才会阻止而不执行上述操作的方法?注意:nginx 中的位置:“location〜\ * ^.+。(jpeg \ | gif \ | png \ | jpg \ | mp4 \ | ......等)”。

点赞
用户6764298
用户6764298

使用 NGINX 的 auth_request 来处理令牌的验证。

http://nginx.org/en/docs/http/ngx_http_auth_request_module.html#auth_request

location /contents/ {
    auth_request /auth;
    ...
}

location = /auth {

    # 在这里验证您的令牌。您可以使用代理请求、njs js_content 或 lua。

    proxy_pass ...
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}
2020-04-28 07:00:28