应用场景是:
用户恶意发起请求,这些请求基本相同,每次请求执行时间为1秒左右。我们想做的事情是:上一次请求未结束时不允许类似的下一次请求进来,用的是shared dict的add一个uid 做的。所以章哥你说得access_by_lua+proxy_pass可以做,但是无法在请求结束的时候删除存储在shared dict中的uid,这样我们的逻辑就一直认为这个用户总有请求正在被处理。所以,有没有proxy_pass之后的处理机制?unicorn里是主业务,
------------------ 原始邮件 ------------------
发送时间: 2013年12月26日(星期四) 3:46
收件人: "openresty" <openresty@googlegroups.com>;
主题: Re: [openresty] ngx.location.capture的疑问
Hello!
2013/12/25 李恒:
> location / {
> if ($request_method ~* GET) {
> proxy_pass http://unicorn;
> }
>
你这里之所以出现无限循环是因为 $request_method 这个变量的值永远是主请求的 method,所以即使你的子请求是 POST
方法,这个 if ($request_method ~* GET) 的条件也永远不会满足。
你可以在这里使用 $echo_request_method 变量,其值总是当前请求(包括子请求)的方法:
https://github.com/agentzh/echo-nginx-module#echo_request_method
你并没有交待你的 http://unicorn 这个服务究竟是用于校验的呢还是校验通过之后访问的主业务。
如果是前者,你这里通过请求方法进行调度是很脆弱的。你这里应当自己为子请求专门定义一个内部使用的 location,比如
location = /auth {
proxy_pass http://unicorn;
}
然后你再在 Lua 里面通过 ngx.location.capture 发起对这个 locatoin 的子请求。
如果你这里的 Lua 代码只是用于验证,则如 Dianyong 指出的,使用 access_by_lua 是更为合适的。假设你对
unicom 的访问是验证通过之后的主业务,则你的配置应当这么写:
location / {
access_by_lua /path/to/auth.lua;
proxy_pass http://unicorn;
}
这样你在 auth.lua 里面根本不用通过 ngx.location.capture 这样的东西发起子请求。
建议首先阅读我的 nginx 教程以避免混乱:http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
你这里的问题在教程里都有比较详细的讲解。
Regards,
-agentzh
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 openresty@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html