My nginx has the following configuration (it forwards and mirrors the parent request to the /pgsql, /mongo locations, and receives the response result of /pgsql)
```conf
http {
......
log_subrequest on;
proxy_buffering off;
proxy_request_buffering off;
......
}
location /test
{
error_log /data/logs/error.log debug;
lua_need_request_body on;
content_by_lua_block {
local reqBody = ngx.req.get_body_data()
local reqUri = ngx.var.uri or ""
local resPg, resMongo = ngx.location.capture_multi( {
{"/pgsql".. reqUri, { method = ngx.HTTP_POST, body = reqBody , args = ngx.var.args, always_forward_body=true }},
{"/mongo".. reqUri , { method = ngx.HTTP_POST, body = reqBody , args = ngx.var.args, always_forward_body=true }}
})
ngx.status = resPg.status
for k, v in pairs(resPg.header) do
ngx.header[k] = v
end
ngx.print(resPg.body)
}
}
location /pgsql
{
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Expose-Headers' '*';
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Expose-Headers' '*';
rewrite ^/pgsql/test$ $original_path break;
proxy_pass http://xxx:xxx/test;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /mongo
{
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Expose-Headers' '*';
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Expose-Headers' '*';
rewrite ^/mongo/test$ $original_path break;
proxy_pass http://xxx:xxx/test;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
......
```
I hope nginx can forward requests in a streaming manner (forward when receiving a request, forward when receiving a response), so I configured proxy_buffering off, proxy_request_buffering off, always_forward_body=true. But after capturing the packet, I found that
- The subrequests of /pgsql and /mongo are forwarded only after receiving the body of the parent request;
- The response result is also forwarded only after receiving the response of /pgsql;
Can someone take a look at and give me some guidance?
Thanks.