这两天也遇到了给subrequest加header的问题,我现在用的方法如下,不修改当前请求,在子请求的location中通过ngx_lua提供的功能添加header,应该算是第一种方法的改良,但不知道效率和第二中方法比如何?
location /sub {
rewrite_by_lua '
ngx.req.set_header("Range", "bytes=2-10");
';
proxy_pass http://127.0.0.1:$server_port/index.html;
}
location /test {
content_by_lua '
local rsp = ngx.location.capture("/sub");
';
}
如果新header的内容不是固定的,可以在当前请求中将其存在ngx.ctx或nginx变量中,然后capture的时候通过ctx或copy_all_vars传到子请求中