matsumotory / ngx_mruby

ngx_mruby - A Fast and Memory-Efficient Web Server Extension Mechanism Using Scripting Language mruby for nginx
https://ngx.mruby.org/
988 stars 112 forks source link

sub request does not work #501

Closed WSandwitch closed 3 months ago

WSandwitch commented 1 year ago

I have this locations in nginx.conf:

location /test3 {
        mruby_content_handler_code '
            args=Nginx::Request.new.args
            Nginx::Async::HTTP.sub_request("/test3/subrequest", args)
            r = Nginx::Async::HTTP.last_response
            Nginx.rputs(r.body)
        ';
    }

    location = /test3/subrequest {
        return 200 "SUBREQUEST $arg_test";
    }

I try to get response from /test3 but with no success:

curl localhost:8000/test3?test=123 -vvv
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /test3?test=123 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.78.0
> Accept: */*
> 

But /test3/subrequest works ok:


curl localhost:8000/test3/subrequest?test=123 -vvv
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /test3/subrequest?test=123 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.78.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.23.1
< Date: Tue, 24 Jan 2023 13:44:50 GMT
< Content-Type: text/plain
< Content-Length: 14
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
SUBREQUEST 123

What am I doing wrong?

matsumotory commented 1 year ago

sub_request method was currently supported in the set, rewrite, and access handlers. For example:

        location /sub_req_proxy_pass {
            proxy_pass http://127.0.0.1:48080/mruby;
            mruby_output_body_filter_code '
              a = 1
            ';
        }

        # non-blocking http client
        location /async_http_sub_request_with_proxy_pass {
            mruby_rewrite_handler_code '
              Nginx::Async::HTTP.sub_request "/sub_req_proxy_pass"
              res = Nginx::Async::HTTP.last_response
              Nginx.rputs res.body
            ';
        }