Hello!
On Tue, Aug 21, 2012 at 8:01 AM, Veniamin Gvozdikov wrote:
> I have question about rewrite_by_lua.
>
> It's my location /test, how to correct convert it to rewrite with lua scripts?
>
> location /test {
> rewrite '^/$' /api?id=1 last;
> rewrite '^/topic/?$' /api?id=1 last;
> rewrite '^/topic/([0-9]+)' /api?id=$1 last;
> rewrite '(.*)' /api?id=error last;
> }
>
I think the location name should be / instead of /test, or it will
always rewrite to /api?id=error, no?
Here is the pure Lua version that achieve the same effect:
location / {
rewrite_by_lua '
local match = ngx.re.match
local set_args = ngx.req.set_uri_args
local uri = ngx.var.uri
if match(uri, "^/$", "jo") then
set_args("id=1")
elseif match(uri, "^/topic/?$", "jo") then
set_args("id=1")
else
local m = match(uri, "^/topic/([0-9]+)", "jo")
if m then
set_args("id=" .. m[1])
else
set_args("id=error")
end
end
return ngx.req.set_uri("/api", true)
';
}
Check out ngx_lua's official documentation for more details:
http://wiki.nginx.org/HttpLuaModule
Best regards,
-agentzh