# Ngx.location.capture\_multi multiple posts

**URL:** <https://www.ruby-forum.com/t/ngx-location-capture-multi-multiple-posts/220755>\
**Category:** NGINX\
**Created:** [June 19, 2012, 4:24pm UTC](https://www.ruby-forum.com/t/ngx-location-capture-multi-multiple-posts/220755 "2012-06-19T16:24:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 19, 2012, 4:24pm UTC](https://www.ruby-forum.com/t/ngx-location-capture-multi-multiple-posts/220755/1 "2012-06-19T16:24:14Z")

</div>

Hello,

I try to use ngx.location.capture\_multi to send multiple post requests:

upstream \_server1 {  
server server1:80;  
}

upstream \_server2 {  
server server2:80;  
}

server {  
listen 10.0.0.254:80;  
server\_name server.internal;

```
location / {
  content_by_lua '
    upstream_servers = {{"/server1", {copy_all_vars = true, method =

```

ngx.HTTP\_POST}},  
{"/server2", {copy\_all\_vars = true, method =  
ngx.HTTP\_POST}},  
}  
for i, v in ipairs(upstream\_servers) do  
upstream\_servers[i][1] = upstream\_servers[i][1] …  
ngx.var.request\_uri  
end  
results = {ngx.location.capture\_multi(upstream\_servers)}  
status = 200  
output = “”  
for i, result in ipairs(results) do  
output = output … result.body  
if status \< result.status then  
status = result.status

```
      end
    end
    ngx.say({output})
    ngx.exit(status)
  ';
}

location /server1 {
  rewrite ^/[^/]*/(.*) /$1 break;
  proxy_pass http://_server1;
  proxy_set_header X-Forwarded-For $remote_addr;
}

location /server2 {
  rewrite ^/[^/]*/(.*) /$1 break;
  proxy_pass http://_server2;
  proxy_set_header X-Forwarded-For $remote_addr;
}

```

}

The post will be send to server1, but the second one won’t be send to  
server2. Afer I while the request terminates with the timeout error 408.  
When I switch the servers in the table uptream\_servers, the request will  
be passed to server2, but not to server1. Again, a timeout occurs. When  
I replace ngx.HTTP\_POST with ngx.HTTP\_GET, both requests will be send.

Is it not possible to use ngx.location.capture\_multi like that?

matthias

---

<div class="post-metadata">

**Author:** ![reima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@reima](https://www.ruby-forum.com/u/reima)\
**Post date:** [June 20, 2012, 8:32am UTC](https://www.ruby-forum.com/t/ngx-location-capture-multi-multiple-posts/220755/2 "2012-06-20T08:32:56Z")

</div>

Hello!

On Tue, Jun 19, 2012 at 10:23 PM, Matthias R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I try to use ngx.location.capture\_multi to send multiple post requests:

[…]

> content\_by\_lua ’  
> upstream\_servers = {{“/server1”, {copy\_all\_vars = true, method =  
> ngx.HTTP\_POST}},  
> {“/server2”, {copy\_all\_vars = true, method =  
> ngx.HTTP\_POST}},  
> }

Here, you let the subrequests all inherit the request body of the  
current request but one gotcha here is that the current request’s body  
can be consumed only once. So you have the following options:

1. Read the entire request body into memory or your custom file using  
ngx\_lua’s ngx.req.get\_body\_data or ngx.req.get\_body\_file API, and then  
feed that to your subrequest by specifying the “body” option value.

2. Read the request body in a streaming fashion by means of the  
“downstream cosocket” object returned by ngx.req.socket(), and send  
the data chunk received to multiple backend HTTP connections at the  
same time by means of the “upstream cosocket” returned by  
ngx.socket.tcp(). In this approach, you’ll no longer use ngx\_proxy.

Best regards,  
-agentzh
