我假设你得到了基础知识到位。 I.E.,您已经在您的服务器上安装了Lua 5.1或更好的LuaJIT 2.0,编译了Nginx和ngx_lua模块,并根据需要配置了ngx_lua。
有了到位,这将做的工作:
location /test {
content_by_lua '
local reqType = ngx.var.request_method
if reqType == ngx.HTTP_POST
OR reqType == ngx.HTTP_DELETE
OR reqType == ngx.HTTP_PUT
then
res = ngx.location.capture("/write_instance")
else
res = ngx.location.capture("/read_instance")
end
ngx.say(res.body)
';
}
location /write_instance {
internal;
proxy_pass http://127.0.0.1:8080;
}
location /read_instance {
internal;
proxy_pass http://127.0.0.1:9080;
}
UPDATE
我想也许你是专门在更大范围内使用Lua。下面的例子也会和limit_except一样工作。
location /test {
if ($request_method !~* GET) {
# For Write Requests
proxy_pass http://127.0.0.1:8080;
}
# For Read Requests
proxy_pass http://127.0.0.1:9080;
}
两者“if”和“limit_except”块有效地创建嵌套位置块,并且一旦条件匹配,仅内容处理程序由此产生的内部位置的块(“proxy_pass”)将被执行。
没有完全知道这是为什么如果有时被认为是“邪恶”,但在这种情况下,“if”和“limit_except”共同的“邪恶”行为可能正是你想要的。
因此有三种选择供您挑选!
但是请注意,如果您需要设置任何其他指令,您将不得不注意“if”或“limit_except”选项中的“邪恶”行为。如果您在“if”或“limit_except”块内设置了一个指令,它可能不在其外部,并且类似地,外部设置的内容可能会被继承。所以你必须看两种方法如何默认继承,或者不是,视情况而定。
If is Evil页面上列出的所有潜在问题均适用于此处的“if”和“limit_except”。基于Lua的脚本编写方法将避免该页面上提出的许多潜在缺陷。
祝你好运!
你能解释一下这个问题好一点?你尝试过这种设置吗?你有任何错误(哪些)? – kikito 2011-12-21 16:22:46