location /image {
  content_by_lua_block {
    local secret_key = "secret_key"
    local uri = ngx.var.uri
    local args = ngx.req.get_uri_args()
    local file_path = string.sub(uri, 5)
    local timestamp = args.t
    local sign = args.sign
    local expected_sign = ngx.md5(file_path .. "@" .. timestamp .. "@" .. secret_key)
    if sign == expected_sign then
      ngx.exec(uri)
    else
      ngx.exit(ngx.HTTP_FORBIDDEN)
    end
  }
}
想要的效果是域名 /文件路径?t=timestamp&sign=hash 值,hash 值为 md5(/文件路径 @时间戳 @密钥)
用 curl 测试的时候一直 403 ,
curl "http://域名 /image/1.jpg?t=$(date +%s)&sign=$(echo -n "/image/1.jpg@$(date +%s)@secret_key" | md5sum | cut -d ' ' -f 1)"
技术水平不到家,望大佬指点
|  |      1proxytoworld      2023-03-29 11:23:32 +08:00 好像你文件路径算错了,不会 lua ,但看你代码算的路径好像不包含 /image ??但请求里的 md5 带了 /image |