# Send all requests to two separate upstream servers?

**URL:** <https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421>\
**Category:** NGINX\
**Created:** [June 17, 2014, 4:14pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421 "2014-06-17T16:14:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [June 17, 2014, 4:14pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/1 "2014-06-17T16:14:29Z")

</div>

I have a need to adjust a nginx install doing reverse proxy to a single  
server now to adjust it to send all requests it receives to two  
different  
upstream servers.

I was thinking I could do it with the configuration below, but I wasn’t  
sure if that would work and I’d have to use re-write rules instead.

upstream original\_upstream {  
server   
}  
upstream new\_upstream {  
server   
}

server {  
location / {  
proxy\_pass http://original\_upstream;  
}  
location / {  
proxy\_pass http://new\_upstream;  
}

Any suggestions?

Eric Feldhusen

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 17, 2014, 5:13pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/2 "2014-06-17T17:13:18Z")

</div>

Option A and that’s what I figured as well.

Eric Feldusen

On Tue, Jun 17, 2014 at 10:08 AM, Richard K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 17, 2014, 5:09pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/3 "2014-06-17T17:09:09Z")

</div>

On 17/06/14 15:13, Eric Feldhusen wrote:

> I have a need to adjust a nginx install doing reverse proxy to a  
> single server now to adjust it to send all requests it receives to two  
> different upstream servers.  
> do you mean  
> a) send each request to both?  
> b) send each request to one or the other (like load balancing)

a) is not possible, simply because of the basics of proxying and http  
(there would be 2 http responses mixed into 1 connection)  
b) can be done with 1 / location but adding another address to the  
upstream block:

|upstream backend {  
server   
||| server |  
}|

|server {  
location / {  
proxy\_passhttp://backend [http://original\_upstream](http://original_upstream);  
}  
}|

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 17, 2014, 5:36pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/4 "2014-06-17T17:36:29Z")

</div>

> Option A and that’s what I figured as well.

Depends on what you actually want to achieve by doing those 2 requests –  
eg  
is it to prewarm 2 backend cache servers or something?

But one way to do this would be for example to use nginx Lua module  
[GitHub - openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers · GitHub](https://github.com/openresty/lua-nginx-module#ngxlocationcapture_multi) /  
ngx.location.capture\_multi , content\_by\_lua etc and then from one  
response  
discard the body ( ngx.req.discard\_body ) or just print the first.

.. theoretically maybe also the Echo module  
[GitHub - openresty/echo-nginx-module: An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file · GitHub](https://github.com/openresty/echo-nginx-module#echo_subrequest) but I’m  
not  
exactly sure how the “combined” response would look like and if the  
duplicate body could be avoided just by sending HEAD request to the  
second  
backend.

You can test it yourself or try to ask agentzh.

rr

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 17, 2014, 5:25pm UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/5 "2014-06-17T17:25:12Z")

</div>

On 17/06/14 16:12, Eric Feldhusen wrote:

> Option A and that’s what I figured as well.

If you don’t care about sending the upstream response back to the  
client, or want to pick one of the two responses to send back  
then you can use the nginx lua module to perform some obscure  
functionality… it’s quite a bit more advanced but something you might  
want to look into

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

however the response would be fully buffered before it could be sent to  
client, so may be quite a delay and a memory hog if it’s something large

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 18, 2014, 12:28am UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/6 "2014-06-18T00:28:05Z")

</div>

Hi

On Tue, Jun 17, 2014 at 3:09 PM, Eric Feldhusen wrote:

> I’m looking for a way to mirror my production site traffic to a development  
> environment, so that I have nearly identical traffic going to both to work  
> through some optimization issues that are hard to do without the load, which  
> is just incoming data.

Sounds like a perfect use case for the tcpcopy tool:

```
https://github.com/wangbin579/tcpcopy

```

Best regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 18, 2014, 5:22am UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/7 "2014-06-18T05:22:03Z")

</div>

That’s almost perfect, except I don’t have enough access to the  
development  
environment to get it installed.

Eric

On Tue, Jun 17, 2014 at 5:27 PM, Yichun Z. (agentzh)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 18, 2014, 5:31am UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/8 "2014-06-18T05:31:33Z")

</div>

You are wanting to multi-purpose a production env for dev without proper  
parameters in ur setup.

Set a system to use as ur test client requesting http, setup an if  
statement and match proper fields and proxy\_pass proxyA or proxy\_pass  
proxyB

Id setup the more specific match on top

Only an idea, im sure there are half a dozen ways of doing this… Just  
not without proper plan

–  
Payam C.  
Network Engineer / Security Specialist

---

<div class="post-metadata">

**Author:** ![Eric\_Feldhusen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/57b2e6/32.png) [@Eric\_Feldhusen](https://www.ruby-forum.com/u/Eric_Feldhusen)\
**Post date:** [June 18, 2014, 12:09am UTC](https://www.ruby-forum.com/t/send-all-requests-to-two-separate-upstream-servers/230421/9 "2014-06-18T00:09:40Z")

</div>

I’m looking for a way to mirror my production site traffic to a  
development  
environment, so that I have nearly identical traffic going to both to  
work  
through some optimization issues that are hard to do without the load,  
which is just incoming data.

Eric
