谢谢
------------------ 原始邮件 ------------------
发件人: "agentzh";<age...@gmail.com>;
发送时间: 2015年8月12日(星期三) 下午5:49
收件人: "openresty"<openresty@googlegroups.com>;
主题: Re: [openresty] 1.7.10.2版openresty——http请求头中传递“If-Match”返回412并且接口报错“no request ctx found”
Hello!
2015-08-12 12:43 GMT+08:00 牛德恒:
>
> nginx.conf中配置如下:
> location /test {
> lua_code_cache off;
> error_log 'logs/test.log' notice;
> default_type 'text/html';
> content_by_lua_file 'lua/test.lua';
> }
>
> test.lua代码如下:
> print("hahhahaah")
> ngx.print("test")
> ngx.exit(200)
>
这是期望的行为。新版的 nginx 会严格地对 If-Match 请求头进行校验和处理。当你的请求指定 If-Match: * 时,就不会返回 412 错误页:
$ curl -H 'If-Match: *'
'127.0.0.1:8080/test?Ing=116.407526&lat=39.90403&e=1439345859'
test
另一种做法是在你为你的响应生成正确的 ETag 响应头,当你生成的 ETag 和 If-Match 中的值匹配时,就不会抛 412
错误页,例如,在你的 ngx.print() 调用(以及 ngx.send_headers 和 ngx.say)之前添加下面这一行:
ngx.header["ETag"] = "c69414b674123bcc5ffd27831371a8c8"
此时就和 If-Match 里指定的值一致了。当然,你不应直接复制 If-Match 请求头的值,那样的话,ETag 就没有意义了。
对于 412 的情况下 ngx.exit() 抛出的“no request ctx found”异常,是因为在发送响应头时,nginx
就立即终止了当前请求,直接返回了 412 响应。因此,正确的做法是总是显式调用 ngx.send_headers()
并且对它返回的错误进行正确的处理,避免在出错时仍继续执行 ngx.print 和 ngx.exit
等调用,因为那样做是没有意义的。简单说来,就是写成下面这个样子:
print("hahhahaah")
-- ngx.header["ETag"] = "c69414b674123bcc5ffd27831371a8c8"
local ok, err = ngx.send_headers()
if not ok then
return
end
ngx.print("test")
ngx.exit(200)
值得一提的是,下回请不要提供截屏。因为我无法从你的截屏图片中复制和粘贴数据,大大增加了我的复现成本和出错机率。谢谢合作!
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