=== TEST 33: headers_sent & HEAD
--- config
location /lua {
content_by_lua '
ngx.say(ngx.headers_sent)
local ok, err = ngx.flush()
if not ok then
ngx.log(ngx.WARN, "failed to flush: ", err)
return
end
ngx.say(ngx.headers_sent)
';
}
--- request
HEAD /lua
--- response_body
--- no_error_log
[error]
--- error_log
failed to flush: header only
=== TEST 34: HEAD & ngx.say
--- config
location /lua {
content_by_lua '
ngx.send_headers()
local ok, err = ngx.say(ngx.headers_sent)
if not ok then
ngx.log(ngx.WARN, "failed to say: ", err)
return
end
';
}
--- request
HEAD /lua
--- response_body
--- no_error_log
[error]
--- error_log
failed to say: header onlyLast-Modified” 或者 “If-Modified-Since”。
Hello!麻烦提供一个最小化的例子吧,能供大家分析使用。
location /test/304 { content_by_lua_block { ngx.status = 200
ngx.header["ETag"] = "\"00f29033f37df21bf5e7d233f1387c67\"" ngx.header["Last-Modified"] = "Wed, 07 Nov 2018 02:34:43 GMT"
ngx.log(ngx.DEBUG, ngx.print(123)) } }curl -i -H "If-Modified-Since:Wed, 07 Nov 2018 02:34:43 GMT" localhost:7000/test/304
HTTP/1.1 304 Not ModifiedServer: openresty/1.13.6.1Date: Wed, 07 Nov 2018 04:05:46 GMTConnection: keep-aliveETag: "00f29033f37df21bf5e7d233f1387c67"Last-Modified: Wed, 07 Nov 2018 02:34:43 GMT
Hello!HTTP 协议里对 304 响应应该是说了不能携带 body 的。因此 nginx 的 ngx_http_header_filter_module 会设置 r->header_only = 1。ngx.print 会在如果头部没发送时先发送头部。
在你这种情况下,ngx.print 内部发现发送完头部后 r->header_only = 1,然后就直接返回了。 ----这里的直接返回,就是说舍弃了上层传入的body??