always_forward_body=true not working

11 views
Skip to first unread message

颜博

unread,
Oct 15, 2024, 5:48:58 AMOct 15
to openre...@googlegroups.com
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;



I saw the documentation description:`always_forward_body` when set to true,...... The request body read by either [ngx.req.read_body()](https://github.com/openresty/lua-nginx-module#ngxreqread_body) or [lua_need_request_body on](https://github.com/openresty/lua-nginx-module#lua_need_request_body) will be directly forwarded to the subrequest without copying the whole request body data when creating the subrequest (no matter the request body data is buffered in memory buffers or temporary files).



Can someone take a look at and give me some guidance?

Thanks.

Junlong li

unread,
Oct 15, 2024, 5:59:05 AMOct 15
to openresty-en
You should submit the issue to https://github.com/openresty/lua-nginx-module.
Reply all
Reply to author
Forward
0 new messages