# How does request.host get set?

**URL:** <https://www.ruby-forum.com/t/how-does-request-host-get-set/143545>\
**Category:** Rails\
**Created:** [July 20, 2008, 9:50am UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545 "2008-07-20T09:50:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 20, 2008, 9:50am UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545/1 "2008-07-20T09:50:05Z")

</div>

how does request.host get set? Is there a way to manually set the  
value in request.host?  
I have several application servers running rails, each has its own  
internal address in the request.host, so when calling redirect\_to or  
url\_for function, it uses the internal address which is not accessible  
to the user.

what I want is to set request.host to return [mysite.com](http://mysite.com), no matter  
which application server it’s running on.

- reynard

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 20, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545/2 "2008-07-20T12:42:40Z")

</div>

On 20 Jul 2008, at 08:49, Reynard wrote:

> how does request.host get set? Is there a way to manually set the  
> value in request.host?

Probably based on environment variables passed through by whatever is  
sitting in front of mongrel (assuming that’s the setup you’re using).  
In something like nginx you can tell it to pass though various headers  
eg:  
proxy\_set\_header Host $http\_host;

Fred

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 20, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545/3 "2008-07-20T16:37:47Z")

</div>

Thanks for the hints. I’m using apache & mongrel. I think I found  
where it gets set, in the cgi\_process.rb, apparently  
env[“HTTP\_X\_FORWARDED\_HOST”] is used to get the host, in my case there  
are 2 values, the public and the private address, it happens to take  
the latter. now I need to find where HTTP\_X\_FORWARDED\_HOST gets set.  
it seems that it should be apache conf thing right?

- reynard

On Jul 20, 6:41 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 20, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545/4 "2008-07-20T17:13:54Z")

</div>

On 20 Jul 2008, at 15:37, Reynard wrote:

> Thanks for the hints. I’m using apache & mongrel. I think I found  
> where it gets set, in the cgi\_process.rb, apparently  
> env[“HTTP\_X\_FORWARDED\_HOST”] is used to get the host, in my case there  
> are 2 values, the public and the private address, it happens to take  
> the latter. now I need to find where HTTP\_X\_FORWARDED\_HOST gets set.  
> it seems that it should be apache conf thing right?

Yup, that should be an apache thing

Fred

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 20, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/how-does-request-host-get-set/143545/5 "2008-07-20T17:57:51Z")

</div>

interesting finding: you have to set ProxyPreserveHost On in Apache,  
so it does not append the local server name.

- reynard

On Jul 20, 11:11 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
