rewrite_by_lua设置在server阶段,查阅相关资料后得知,它的作用是每个location 阶段都执行rewrite_by_lua_block 的代码逻辑。
最后发现,下面的这段示例没有问题,可一旦在location 阶段进行rewrite就会发现不起作用。
server {
set $foo '1';
rewrite_by_lua 'ngx.var.foo = "2"';
location / {
if ($foo = '1') { return 404; }
if ($foo = '2') { return 503; }
}
}
示例1:不能正常访问,客户端浏览器一直等待服务器返回
rewrite_by_lua_no_postpone on;
server {
listen 80;
server_name *.test.io;
access_log logs/test_access.log main;
error_log logs/test_error.log debug;
root html;
set $foo "";
rewrite_by_lua_block {
local hostName = ngx.var.http_host
if(hostName=='www.test.io')
then
ngx.var.foo="a.html"
else
ngx.var.foo="b.html"
end
}
location = / {
rewrite ^/(.*)$ /$foo last;
}
}
等同于示例1的写法,下面的
示例2,客户端浏览器访问正常。
rewrite_by_lua_no_postpone on;
server {
listen 80;
server_name *.test.io;
access_log logs/test_access.log main;
error_log logs/test_error.log debug;
root html;
set $foo "";
location = / {
rewrite_by_lua_block {
local hostName = ngx.var.http_host
if(hostName=='www.test.io')
then
ngx.var.foo="a.html"
else
ngx.var.foo="b.html"
end
}
rewrite ^/(.*)$ /$foo last;
}
}
请问,上面的问题是我理解有问题,还是ngx_lua的bug,
ngx_lua 版本 0.10.7