虽然有点晚了,但这里是:
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!";
}