Hello!
2014-03-18 20:34 GMT-07:00 Kaito Sys:
> ngx.say(query);
> local res = ngx.location.capture("/getxxxx",{ method = ngx.HTTP_POST, body =
> query})
> query打印了肯定有值
>
> 现在观察到这种现象:
> 用get方式请求A
> query打印:ip=1.2.2.255&uid=1883024754
> res.body打印:"uid":"null"
>
我猜你的后端应用(比如 PHP 应用)需要 POST 请求携带有请求头 Content-Type:
application/x-www-form-urlencoded
而对于你发起的 GET 主请求,自然不会有这个 Content-Type 请求头,而 ngx.location.capture
发起的子请求是自动继承主请求的请求头的。
一个很简单的解决办法是在你的子请求指向的 location = /getxxxx 里加上下面这一行配置:
proxy_set_header Content-Type "application/x-www-form-urlencoded";
当然,你也可以把这里的请求头的值弄成变量,然后从父请求传递值。
> 用post方式请求A
> query打印:ip=1.2.2.255&uid=1883024754
> res.body打印:"uid":"1883024754"
>
我不清楚你这里的“post方式”具体是怎么弄的,但我猜此时你的 http 客户端应该发送了 Content-Type:
application/x-www-form-urlencoded 这个请求头。你可以通过 nc 或者 tcpdump 这样的工具在 TCP
的层面上加以确认。
Regards,
-agentzh