# Help with setting proxy\_header

**URL:** <https://www.ruby-forum.com/t/help-with-setting-proxy-header/218203>\
**Category:** NGINX\
**Created:** [March 24, 2012, 1:40am UTC](https://www.ruby-forum.com/t/help-with-setting-proxy-header/218203 "2012-03-24T01:40:48Z")\
**Posts on this page:** 3\
**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:** [March 24, 2012, 1:40am UTC](https://www.ruby-forum.com/t/help-with-setting-proxy-header/218203/1 "2012-03-24T01:40:48Z")

</div>

Hello!

I have a reverse proxy setup like this…

server {  
…  
…

```
         if ($host = '23.71.33.142' ) { proxy_set_header Host 

```

[www.domain.com](http://www.domain.com); }  
location / { proxy\_pass [http://31.186.1.131:60/](http://31.186.1.131:60/); }

}

The problem is that i cannot have a proxy\_set\_header inside an IF like  
this. Basically what iam intending to do is… If the remote client  
visits my server with the IP (23.71.33.142) as the header (by using  
[http://23.71.33.142](http://23.71.33.142)) I need to send hardcoded the header to the backend  
server si my backend server knows it should respond with the content of  
[domain.com](http://domain.com) . If they send any other header then it needs to be past to  
the backend server to deal with intact (it can be [domain.com](http://domain.com) /  
[www.domain.com](http://www.domain.com) / [sub.domain.com](http://sub.domain.com) )

Thanks!

–Mike

---

<div class="post-metadata">

**Author:** ![Micheal\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Micheal\_W](https://www.ruby-forum.com/u/Micheal_W)\
**Post date:** [March 24, 2012, 8:58pm UTC](https://www.ruby-forum.com/t/help-with-setting-proxy-header/218203/2 "2012-03-24T20:58:41Z")

</div>

On Sat, Mar 24, 2012 at 12:40:06AM +0000, Micheal W. wrote:

Hi there,

> ```
> if ($host = '23.71.33.142' ) { proxy_set_header Host 
> 
> ```

[www.domain.com](http://www.domain.com); }

> ```
> location / { proxy_pass http://31.186.1.131:60/; }
> 
> ```

You want to have

proxy\_set\_header Host $host;

unless $host is this IP address, in which case you want a different  
value?

set $myhost $host;  
if ($host = ‘23.71.33.142’) { set $myhost [www.domain.com](http://www.domain.com); }  
proxy\_set\_header Host $myhost;

where the first two are at server{} level, and the last is at server{}  
or location{} level.

Using the “map” directive is another way of setting your $myhost  
variable.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Micheal\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@Micheal\_W](https://www.ruby-forum.com/u/Micheal_W)\
**Post date:** [March 26, 2012, 1:22pm UTC](https://www.ruby-forum.com/t/help-with-setting-proxy-header/218203/3 "2012-03-26T13:22:45Z")

</div>

> ```
> location / { proxy_pass http://31.186.1.131:60/; }
> 
> ```
> 
> Thanks!
> 
> –Mike

The more proper way is to use ‘map’ directive:

map $remote\_addr $myhost {  
default $http\_host;  
23.71.33.142 [www.domain.com](http://www.domain.com);  
}

server {  
…  
location / {  
proxy\_set\_header Host $myhost;  
}  
…  
}
