Hello!
On Sun, Oct 20, 2013 at 8:44 AM, 力童 wrote:
> 我正在使用ngx_lua + memc + upload_module 实现一个动态上传限速功能;
>
其实你可以很容易地单纯使用 ngx_lua 模块配合 lua-resty-memcached 和 lua-resty-upload
这两个库来完整的实现这里的功能(无需使用 ngx_memc 模块和 ngx_upload 模块)。
事实上,这也是推荐的做法,无论是从简单性还是整体性能方面考虑。
相关细节请见
https://github.com/agentzh/lua-resty-memcached
https://github.com/agentzh/lua-resty-upload
> 思路是:发现如果在memc中黑名单存在id,则配置限速 upload_limit_rate 50k,否则默认512k
>
> 问题出现在:不确定upload_limit_rate 配置是在nginx的哪个阶段,发现无论怎么写,都无法生效,所以想请教一下;我写的配置如下:
>
> set $limit "unlimit";
> rewrite_by_lua '
> local id = tonumber(ngx.var.arg_id)
>
> if id == nil then
> return
> end
>
> local res = ngx.location.capture("/memcache?id=" .. id)
>
> if res.status == ngx.HTTP_OK then
> ngx.var.limit = "limited"
> end
> ';
>
> if ($limit = "limited")
> {
> upload_limit_rate 10k;
> }
> if ($limit = "unlimit")
> {
> upload_limit_rate 512k;
> }
>
你这个配置有一个明显的错误。rewrite_by_lua 指令总是运行在 ngx_rewrite 模块的所有指令(包括 if)之后,即使你把前者写在后者的前面。
另外,建议尽量不要使用 nginx 的 if,因为“if 是邪恶的”,很容易搞错。这也是为什么我建议你使用纯 Lua 的方案。
同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty
建议你也加入此列表,并在那里讨论这样的问题。谢谢合作!
Regards,
-agentzh