# How can a rails app get its host name?

**URL:** <https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642>\
**Category:** rubyonrails-talk\
**Created:** [November 19, 2009, 7:55pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642 "2009-11-19T19:55:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Burgett](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@Ken\_Burgett](https://discuss.rubyonrails.org/u/Ken_Burgett)\
**Post date:** [November 19, 2009, 7:55pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/1 "2009-11-19T19:55:50Z")

</div>

I would like my Rails app to supply a callback url to another app. I currently have a yaml file in the config folder in which I put the host name, but I would like to be able to query that dynamically and do away with the yaml file. Is there a method call that returns the current host?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 19, 2009, 7:57pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/2 "2009-11-19T19:57:49Z")

</div>

Sort of - a given request has a host name (look at the methods on ActionController::Request), obviously you can only get at that inside a controller action.

Fred

---

<div class="post-metadata">

**Author:** ![Owain](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@Owain](https://discuss.rubyonrails.org/u/Owain)\
**Post date:** [November 19, 2009, 11:44pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/3 "2009-11-19T23:44:03Z")

</div>

request.host

---

<div class="post-metadata">

**Author:** ![Norm2](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@Norm2](https://discuss.rubyonrails.org/u/Norm2)\
**Post date:** [November 19, 2009, 11:47pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/4 "2009-11-19T23:47:31Z")

</div>

explainer wrote:

> ```ruby
> 
> I would like my Rails app to supply a callback url to another app. I
> currently have a yaml file in the config folder in which I put the
> host name, but I would like to be able to query that dynamically and
> do away with the yaml file. Is there a method call that returns the
> current host?
> --
> 
> ```

You should be able to do "name = system ‘hostname’ " if you are on a linux host. This will not give the name used by a virtual host though.

---

<div class="post-metadata">

**Author:** ![shweta](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@shweta](https://discuss.rubyonrails.org/u/shweta)\
**Post date:** [November 20, 2009, 6:06am UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/5 "2009-11-20T06:06:03Z")

</div>

Try request.env['HTTP\_HOST'] in the controller.

---

<div class="post-metadata">

**Author:** ![Scott\_Johnson1](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@Scott\_Johnson1](https://discuss.rubyonrails.org/u/Scott_Johnson1)\
**Post date:** [November 20, 2009, 7:03pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/6 "2009-11-20T19:03:38Z")

</div>

That assumes you are on the default port (80).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://discuss.rubyonrails.org/u/Brian_Candler)\
**Post date:** [November 20, 2009, 9:05pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/7 "2009-11-20T21:05:08Z")

</div>

To find what the machine thinks its own name is (which may or may not be related to the hostname you want in a callback url), try:

&nbsp;&nbsp;Socket.gethostname

As has been pointed out, the callback url also needs things like the port. If you are generating this within a controller action, you could of course just use the url\_for() helper to generate the callback url.

---

<div class="post-metadata">

**Author:** ![sami](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sami](https://discuss.rubyonrails.org/u/sami)\
**Post date:** [November 21, 2009, 4:00pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/8 "2009-11-21T16:00:58Z")

</div>

request.env["SERVER\_ADDR"] // this will return you server ip address request.server\_name or request.host // this will return host name

---

<div class="post-metadata">

**Author:** ![Eric9](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@Eric9](https://discuss.rubyonrails.org/u/Eric9)\
**Post date:** [November 21, 2009, 6:13pm UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/9 "2009-11-21T18:13:25Z")

</div>

An example that has caused me to have to use a config.yml, ActionMailer will not have request.\* available. System calls would not work in this case either, since they would return a bad value on vhosts or in cases where the hostname is different than request.host.

-eric

---

<div class="post-metadata">

**Author:** ![tommy\_xiao](https://avatars.discourse-cdn.com/v4/letter/t/b77776/32.png) [@tommy\_xiao](https://discuss.rubyonrails.org/u/tommy_xiao)\
**Post date:** [November 25, 2009, 4:30am UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/10 "2009-11-25T04:30:26Z")

</div>

request.host\_with\_port()

in rails 2.3.4 api

---

<div class="post-metadata">

**Author:** ![Andres](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@Andres](https://discuss.rubyonrails.org/u/Andres)\
**Post date:** [November 25, 2009, 6:01am UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/11 "2009-11-25T06:01:08Z")

</div>

> ```ruby
> I would like my Rails app to supply a callback url to another app. I
> currently have a yaml file in the config folder in which I put the
> host name, but I would like to be able to query that dynamically and
> do away with the yaml file. Is there a method call that returns the
> current host?
> 
> ```

probably you want to write a little rack middleware app here,

check this link and the comments with code underneath,

[http://coderack.org/users/laktek/entries/11-server-proxy](http://coderack.org/users/laktek/entries/11-server-proxy)

---

<div class="post-metadata">

**Author:** ![INDRANIL\_MUKHERJEE](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@INDRANIL\_MUKHERJEE](https://discuss.rubyonrails.org/u/INDRANIL_MUKHERJEE)\
**Post date:** [November 25, 2009, 8:14am UTC](https://discuss.rubyonrails.org/t/how-can-a-rails-app-get-its-host-name/47642/12 "2009-11-25T08:14:43Z")

</div>

check database.yml file in config directory
