如何使用openresty'实现uwsgi#U通行证;什么是Lua插件?
我需要对匹配特定位置的传入请求执行一些半复杂的逻辑。简而言之,对于符合如何使用openresty'实现uwsgi#U通行证;什么是Lua插件?,lua,uwsgi,openresty,Lua,Uwsgi,Openresty,我需要对匹配特定位置的传入请求执行一些半复杂的逻辑。简而言之,对于符合位置~*“^/([0-9a-f]{8}-([0-9a-f]{4}-{3}[0-9a-f]{12}/?$”的所有URL,需要进行以下操作: 检查是否存在管理员cookie。如果是: 重写URL(/->/mod/) 执行uwsgi\u通行证 其他: 在postgres中查找与UUID匹配的条目 在条目中搜索可用的重定向URL 将客户端重定向到选定的URL 除了uwsgi\u pass位之外,所有这些都可以通过使用conte
位置~*“^/([0-9a-f]{8}-([0-9a-f]{4}-{3}[0-9a-f]{12}/?$”
的所有URL,需要进行以下操作:
- 重写URL(
->/
)/mod/
- 执行
uwsgi\u通行证
- 在postgres中查找与UUID匹配的条目
- 在条目中搜索可用的重定向URL
- 将客户端重定向到选定的URL
uwsgi\u pass
位之外,所有这些都可以通过使用content\u by\u lua\u块来实现;事实证明,谷歌在这一努力中毫无帮助
我如何通过lua\u块在内容中执行uwsgi\u传递
?虽然有点晚,但以下是:
location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$" {
set $uuid $1;
rewrite_by_lua '
local new_url
if (ngx.var.cookie_admin) then
new_url = "/modif/" .. ngx.var.uuid
else
--Get the url from DB using uuid
local res = ngx.location.capture("/url/" .. ngx.var.uuid);
if (res.status == 200 and res.body) then
new_url = tostring(res.body) --change the uri to the one from DB
else
return ngx.exec("@notfound") -- Fallback locaton if no url found in DB
end
end
ngx.req.set_uri(new_url) -- rewrite uri to new one
';
echo $uri; #test
#uwsgi_pass upstream_server;
}
它运行良好,有两个测试位置:
location ~ "^/url/(.+)$" {
internal;
set $uuid $1;
#return 410; # test Uncomment to test @notfound
echo "/url/from/db/$uuid"; #test
#assuming you have postgre module
#postgres_pass database;
#rds_json on;
#postgres_query HEAD GET "SELECT url FROM your_table WHERE uuid='$uuid'";
#postgres_rewrite HEAD GET no_rows 410;
}
location @notfound {
echo "Ping! You got here because you have no cookies!";
}
为什么您需要在内容中使用uwsgi\u pass
?根据您的需求,1和2流似乎相互排斥。流程2:postgres查找并生成重定向。此外,一个“按lua重写”阶段处理程序似乎更合适。@danielgpm,它本身不是必需的,我只是假设在一个“按lua重写内容”块中实现所有这些逻辑会更容易。你能详细说明你提出的解决方案吗?我很乐意接受这个答案,一点也不晚!你的时机真的很好,因为我刚刚又回到这个话题上。谢谢