在 2014年1月9日星期四UTC+8上午11时45分13秒,agentzh写道:
>
> 2014/1/8 yhben:
> > 事情是这样的, 我在使用 Nginx 的过程中, 想实现通过 proxy_pass 下载页面, 然后使用 lua 的 body_filter 里调用
> > capture, 将得到的 html 通过 http post, 传输到我们的工具进行处理, 再输出给客户端.
> > 但是实际上, 无法在 body_filter 中调用 capture, 所以想咨询一下, 应该如何解决掉这个问题.
>
> Nginx 的 body filter 里是不支持非阻塞 IO 的。推荐的做法是在在子请求里做原先主请求的
>
> proxy_pass,主请求中使用 ngx.location.capture,对响应进行全缓冲处理,再在主请求中使用 ngx.print()
>
> 一次性输出结果。
>
> -agentzh
当我使用主请求 capture 到子请求时, 我该如何在子请求里拿到主请求的上下文环境呢?
location /proxy {
internal;
proxy_pass http://www.baidu.com:80;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Forwarded-For $remote_addr;
more_clear_headers 'Content-Length';
}
location / {
content_by_lua '
res = ngx.location.capture("/proxy");
ngx.status = res.status;
for key, value in pairs(res.header) do ngx.header[key] = value end;
ngx.print(res.body)';
}