1

我需要对与特定位置匹配的传入请求执行一些半复杂的逻辑。简而言之,对于所有符合 的 url location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/?$",需要进行以下操作:

  1. 检查是否存在管理员 cookie。如果是这样的话:
    • 重写 URL ( /<uuid>-> /mod/<uuid>)
    • 履行uwsgi_pass
  2. 别的:
    • 在 postgres 中查找匹配 UUID 的条目
    • 在条目中搜索可用的重定向 URL
    • 将客户端重定向到选定的 URL

content_by_lua_block除了uwsgi_pass位之外,所有这些都相当简单。事实证明,谷歌在这项工作中最没有帮助......

我怎样才能执行 a uwsgi_passin acontent_by_lua_block

4

1 回答 1

2

虽然有点晚了,但这里是:

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!";
}
于 2016-02-14T00:07:08.290 回答