ngx.location.capture 发送子请求 到proxy_cache

410 views
Skip to first unread message

韩世林

unread,
Jul 16, 2015, 3:56:55 AM7/16/15
to open...@googlegroups.com
你好
    我在尝试调用local res = ngx.location.capture("/foo",{ctx = para}) 发送子请求取本地的缓存文件, 然后对内容进行一些修改
    rewrite.lua:
             local para = {realuri = ngx.var.uri}
             local res = ngx.location.capture("/foo",{ctx = para})
             ngx.log(ngx.DEBUG,"status:"..res.status.."\tbodysize:"..string.len(res.body))

   配置里的内容是这样的:

            location  /faa {
                 rewrite_by_lua_file conf/luaconfig/rewrite.lua;
             }

             location /foo{
                 rewrite_by_lua '
                     ngx.req.set_uri(ngx.ctx.realuri)
                 ';
                 proxy_cache cache_one;
                 proxy_cache_use_stale updating;
                 proxy_cache_valid  200 304 365d;
                 proxy_cache_key $uri
                 proxy_set_header Host  $host;
                 proxy_pass http://localhost;
            }
  
从日志里看到 status是200,但是却取不到body, lua线程退出了。 如果我把proxy_cache 去掉, 直接proxy_pass到后端 我可以取到body。
难道是proxy_cache 模式有什么限制吗?

Yichun Zhang (agentzh)

unread,
Jul 16, 2015, 10:53:49 PM7/16/15
to openresty
Hello!

2015-07-16 15:56 GMT+08:00 韩世林:
> 从日志里看到 status是200,但是却取不到body, lua线程退出了。 如果我把proxy_cache 去掉, 直接proxy_pass到后端
> 我可以取到body。
> 难道是proxy_cache 模式有什么限制吗?
>

我们公司的 Bag API 这个服务就是使用的 ngx.location.capture + proxy_pass + proxy_cache :)

调试这样的问题,建议开启 nginx 调试日志进行分析:

http://nginx.org/en/docs/debugging_log.html

Regards,
-agentzh

韩世林

unread,
Jul 16, 2015, 11:15:53 PM7/16/15
to open...@googlegroups.com
你好,
     我从日志里看到, 确实是从cache找到缓存文件了
2015/07/17 11:08:46 [debug] 32481#0: *534 lua run post subrequest handler, rc:-2 c:2
2015/07/17 11:08:46 [debug] 32481#0: *534 event timer add: 14: 60000:1437102586841
2015/07/17 11:08:46 [debug] 32481#0: *534 http posted request: "/BEB20A14DA5AA173125CC38A8AB0FF88_20150606_1_1_255.mp4/playlist.m3u8?arange=100"
2015/07/17 11:08:46 [debug] 32481#0: *534 rewrite phase: 5
2015/07/17 11:08:46 [debug] 32481#0: *534 lua rewrite handler, uri:"/BEB20A14DA5AA173125CC38A8AB0FF88_20150606_1_1_255.mp4/playlist.m3u8" c:2
2015/07/17 11:08:46 [debug] 32481#0: *534 lua run subrequests done, resuming lua thread
2015/07/17 11:08:46 [debug] 32481#0: *534 lua handle subrequest responses
2015/07/17 11:08:46 [debug] 32481#0: *534 lua run thread, top:0 c:2
2015/07/17 11:08:46 [debug] 32481#0: *534 [lua] rewrite_cc_M3U8.lua:44: REWRITE M3U8 status:200 bodysize:0
感觉没什么异常的地方呀, 附件是完整的日志, 给我的感觉好像就是proxy_cache 作为子请求发送不了body

在 2015年7月17日星期五 UTC+8上午10:53:49,agentzh写道:
80_error.log

Yichun Zhang (agentzh)

unread,
Jul 17, 2015, 12:08:23 AM7/17/15
to openresty
Hello!

2015-07-17 11:15 GMT+08:00 韩世林:
> 我从日志里看到, 确实是从cache找到缓存文件了
[...]
> 感觉没什么异常的地方呀, 附件是完整的日志, 给我的感觉好像就是proxy_cache 作为子请求发送不了body
>

从你提供的调试日志中看到一些潜在的问题:

1. 你目前使用的是 Tengine,而我并不会对使用了 Tengine 的情形提供技术支持。我只支持 OpenResty 自带的
NGINX 和官方的 NGINX。请尝试使用最新的 OpenResty 正式版发布,看是否仍有问题。值得一提的是,Tengine 基于的
NGINX 核心和 ngx_lua 模块经常都比较古老,很可能含有官方早已修复了的 bug.
2. 我看到你开启了 AIO,我不确定这是否会有问题,因为我从未测试过此种情况。建议你尝试关闭 AIO
看看是否仍有问题。值得一提的是,如果你在 Linux 上,则为 proxy_cache 启用 AIO 很可能会降低性能(因为 Direct
IO 总是会绕过 page cache)。

Regards,
-agentzh

韩世林

unread,
Jul 17, 2015, 12:20:05 AM7/17/15
to open...@googlegroups.com
你好
    server我用的是是openresty 1.7.10.2 ,后端是tenginx的版本, 我取文件是从本地取的,应该是不会从后端tenginx进行交互。
    

在 2015年7月17日星期五 UTC+8下午12:08:23,agentzh写道:

韩世林

unread,
Jul 17, 2015, 12:30:18 AM7/17/15
to open...@googlegroups.com
当我把aio 关掉了之后,  问题就解决了, 看来真是aio会有影响。


在 2015年7月17日星期五 UTC+8下午12:08:23,agentzh写道:
Hello!

Yichun Zhang (agentzh)

unread,
Jul 17, 2015, 1:25:15 AM7/17/15
to openresty
Hello!

2015-07-17 12:30 GMT+08:00 韩世林:
> 当我把aio 关掉了之后, 问题就解决了, 看来真是aio会有影响。
>

OK,我大致知道是什么原因了,我回头修一下这个问题。届时你再帮我测试一下看看?

多谢报告!

Best regards,
-agentzh

韩世林

unread,
Jul 17, 2015, 1:42:07 AM7/17/15
to open...@googlegroups.com
好的

在 2015年7月17日星期五 UTC+8下午1:25:15,agentzh写道:
Reply all
Reply to author
Forward
0 new messages