# Condition in upstream

**URL:** <https://www.ruby-forum.com/t/condition-in-upstream/222856>\
**Category:** NGINX\
**Created:** [September 12, 2012, 10:32pm UTC](https://www.ruby-forum.com/t/condition-in-upstream/222856 "2012-09-12T22:32:29Z")\
**Posts on this page:** 3\
**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:** [September 12, 2012, 10:32pm UTC](https://www.ruby-forum.com/t/condition-in-upstream/222856/1 "2012-09-12T22:32:29Z")

</div>

Hi guys,

I’m studying about in the use of upstream in the nginx. And I have a  
question about the condition of use it.

Now, I have the following environment in production:  
Internet – \> Nginx → Varnish → Apache

I would like of set in upstream a backup’s server, for example:  
upstream backend {  
ip\_hash;  
server 192.168.1.5:6081; # varnish server  
server 192.168.1.7:80 backup; # apache server  
}

But in the case the configuration of the “location /” will change  
according  
with the server.

Is there some parameter of condition that I can used?

Best regards,  
Thiago Gomes

Posted at Nginx Forum:

> **[Condition in upstream](https://forum.nginx.org/read.php?2,230678,230678#msg-230678)**
>
> Hi guys, I'm studying about in the use of upstream in the nginx. And I have a question about the condition of use it. Now, I have the following environment in production: Internet -- \> Nginx --\> Varnish --\> Apache I would like of set in up

---

<div class="post-metadata">

**Author:** ![thigomes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@thigomes](https://www.ruby-forum.com/u/thigomes)\
**Post date:** [September 12, 2012, 11:05pm UTC](https://www.ruby-forum.com/t/condition-in-upstream/222856/2 "2012-09-12T23:05:12Z")

</div>

Just to complete the information, in my case I have two configuration  
according with the web server, so:

# TO VARNISH

location / {  
proxy\_redirect off; # Do not redirect this proxy - It needs to be  
pass-through  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header X-Server-Address $server\_addr;  
proxy\_connect\_timeout 5s;  
proxy\_read\_timeout 300s;  
proxy\_pass\_header Set-Cookie;  
proxy\_pass [http://localhost:6081](http://localhost:6081); # Pass all traffic through to  
Varnish  
}

TO APACHE  
location / {  
client\_max\_body\_size 10m;  
client\_body\_buffer\_size 128k;  
proxy\_send\_timeout 90;  
proxy\_read\_timeout 90;  
proxy\_buffer\_size 32k;  
proxy\_buffers 8 16k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;  
proxy\_connect\_timeout 30s;  
proxy\_redirect [http://domain.com:8081](http://domain.com:8081) [http://domain.com](http://domain.com);  
proxy\_redirect [http://www.domain.com:8081](http://www.domain.com:8081) [http://www.domain.com](http://www.domain.com);

proxy\_pass [http://192.168.1.7:8081/](http://192.168.1.7:8081/);  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
}

In the case varnish server not response, the request will forward to  
apache  
server.

Att,  
Thiago Gomes

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,230678,230680#msg-230680](http://forum.nginx.org/read.php?2,230678,230680#msg-230680)

---

<div class="post-metadata">

**Author:** ![thigomes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@thigomes](https://www.ruby-forum.com/u/thigomes)\
**Post date:** [September 13, 2012, 9:30am UTC](https://www.ruby-forum.com/t/condition-in-upstream/222856/3 "2012-09-13T09:30:59Z")

</div>

On Sep 13, 2012, at 1:04 , thigomes wrote:

> proxy\_set\_header X-Server-Address $server\_addr;  
> proxy\_send\_timeout 90;  
> proxy\_set\_header Host $host;  
> proxy\_set\_header X-Real-IP $remote\_addr;  
> proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
> }
> 
> In the case varnish server not response, the request will forward to apache  
> server.

Backup servers are not supported with ip\_hash upstream.  
You should use something like this:

upstream backend {  
server 192.168.1.5:6081; # varnish server  
server 192.168.1.7:80 backup; # apache server  
}

server {  
location / {  
proxy\_pass [http://backend](http://backend);  
…  
}  
}

However, you can omit varnish at all and use nginx built-in cache:

proxy\_cache\_path /data/nginx/cache levels=1:2 keys\_zone=CACHE:10m;

server {  
location / {  
proxy\_pass [http://192.168.1.7](http://192.168.1.7);  
proxy\_cache CACHE;  
…  
}  
}

–  
Igor S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
