# Upstream host name or address

**URL:** <https://www.ruby-forum.com/t/upstream-host-name-or-address/208465>\
**Category:** NGINX\
**Created:** [June 19, 2011, 1:27pm UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465 "2011-06-19T13:27:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 19, 2011, 1:27pm UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/1 "2011-06-19T13:27:40Z")

</div>

hi,  
is it possible to find upstream host which serves as proxy for a  
connection;  
i know that $upstream\_addr is the key ; but i need to set the address  
with set\_proxy\_headdr as host  
but $upstream\_addr return address and port together

upstream test  
{  
ip\_hash;  
192.168.1.1;  
192.168.1.2;  
}

location /  
{  
proxy\_set\_header Host $upstream\_addr; ---------\> fail cause it set for  
example 192.168.1.1:80 but i need 192.168.1.1  
proxy\_pass [http://test](http://test);  
}

any suggestion ?

thank you in advance…

Posted at Nginx Forum:

> **[upstream host name or address](https://forum.nginx.org/read.php?2,208188,208188#msg-208188)**
>
> hi, is it possible to find upstream host which serves as proxy for a connection; i know that $upstream\_addr is the key ; but i need to set the address with set\_proxy\_headdr as host but $upstream\_addr return address and port together upstream test { i

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 19, 2011, 1:43pm UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/2 "2011-06-19T13:43:43Z")

</div>

i forgot to add server in above upstream samples;;but my main config is  
correct;  
i just wanted to show an example of what i wanna do…  
upstream test  
{  
ip\_hash;  
server 192.168.1.1;  
server 192.168.1.2;  
}

thanks again;  
i hope someone help me…

Posted at Nginx Forum:

> **[Re: upstream host name or address](https://forum.nginx.org/read.php?2,208188,208192#msg-208192)**
>
> hi, is it possible to find upstream host which serves as proxy for a connection; i know that $upstream\_addr is the key ; but i need to set the address with set\_proxy\_headdr as host but $upstream\_addr return address and port together upstream test { i

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 19, 2011, 11:40pm UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/3 "2011-06-19T23:40:34Z")

</div>

Hello!

On Sun, Jun 19, 2011 at 07:26:58AM -0400, torajx wrote:

> 192.168.1.1;
> 
> any suggestion ?

Request is created only once and you can’t set different headers  
when working with different servers in upstream. Servers in  
upstream block must accept identical requests.

(Moreover, $upstream\_addr isn’t an address of upstream server nginx is  
talking to, but a list of servers nginx have talked to during  
handling of a request. And usually it won’t contain anything  
while creating request to upstream.)

If you really need different requests to different backend servers  
you’ll have to use different proxy\_pass’es for that (or use  
variables). E.g. in 0.9.6+ you may do something like this:

```
map $remote_addr $backend {
    default "should not happen";
    ~[02468]\.\d+$ 192.168.1.1;
    ~[13579]\.\d+$ 192.168.1.2;
}

location / {
    proxy_pass http://$backend;
}

```

(no need for proxy\_set\_header here, as $proxy\_host will resolve to  
the backend’s ip address and default “proxy\_set\_header Host  
$proxy\_host;” will do what you want)

But note that recommended aproach is to actually configure  
identical backend hosts, not adapt requests for each one.

Maxim D.

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 20, 2011, 6:59am UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/4 "2011-06-20T06:59:53Z")

</div>

by the way nx.chare.ir in my nginx server and i forgot to remove real  
name at the end…no problem 😉  
it means users can type nx.chare.ir/csupdate  
and in csupdate there is some link to images that i mention in images  
location…

Posted at Nginx Forum:

> **[Re: upstream host name or address](https://forum.nginx.org/read.php?2,208188,208380#msg-208380)**
>
> hi, is it possible to find upstream host which serves as proxy for a connection; i know that $upstream\_addr is the key ; but i need to set the address with set\_proxy\_headdr as host but $upstream\_addr return address and port together upstream test { i

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 20, 2011, 8:24am UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/5 "2011-06-20T08:24:34Z")

</div>

sorry againa and again  
i forgot one line again i have this line in my csupdate location too’  
proxy\_redirect [http://192.168.1.1/csupdate](http://192.168.1.1/csupdate)  
[http://nx.chare.ir/csupdate](http://nx.chare.ir/csupdate);

my csupdate location is like this finally :

location /csupdate {  
proxy\_pass http://mysites\_hash/csupdate;  
proxy\_set\_header Host 192.168.1.1;  
proxy\_redirect [http://192.168.1.1/csupdate](http://192.168.1.1/csupdate)  
[http://nx.chare.ir/csupdate](http://nx.chare.ir/csupdate);  
}

but this works when i have only one server in mysites\_has upstream

Posted at Nginx Forum:

> **[Re: upstream host name or address](https://forum.nginx.org/read.php?2,208188,208391#msg-208391)**
>
> hi, is it possible to find upstream host which serves as proxy for a connection; i know that $upstream\_addr is the key ; but i need to set the address with set\_proxy\_headdr as host but $upstream\_addr return address and port together upstream test { i

---

<div class="post-metadata">

**Author:** ![torajx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@torajx](https://www.ruby-forum.com/u/torajx)\
**Post date:** [June 20, 2011, 6:57am UTC](https://www.ruby-forum.com/t/upstream-host-name-or-address/208465/6 "2011-06-20T06:57:24Z")

</div>

hi again and thank you fo reply  
this is my main config file; sorry i remove real ip address and site  
names

upstream mysites {  
server 192.168.1.1 weight=5;

# server 192.168.1.2 weight=3;

}  
upstream mysites\_hash {  
ip\_hash; #base on clinet ip address and can not use weight;  
server 192.168.1.1;

# server 192.168.1.2;

}  
upstream myimages {  
server [images.mysite.com](http://images.mysite.com);  
}  
server {  
listen 80;  
server\_name nx.chare.ir;  
set $tmp tempstr;  
access\_log var/log/nx\_csupdate\_access.log main;  
log\_format main “$upstream\_addr”;

```
   location / {
         deny all;
   }
   location /csupdate/images {
         proxy_pass http://myimages;
   }
   location /csupdate {
      proxy_pass http://mysites_hash;
      proxy_set_header Host 192.168.1.1;
   }

```

as you can see i commented my 2th server in my upstream;  
the proxy\_set\_header line in location /csupdate was the only soloution  
that solved  
my problem; because my servers (192.168.1.1, 2) will redirect users base  
on some condition  
and without proxy\_set\_header line the browsers fall in endless loop and  
retun errors;

for $upstream\_addr in can see 192.168.1.1:80 in my log files as i  
formated simply likme above;  
but what is soloution in i want to use 192.168.1.2 server too ??

thank you in advance

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