# Getting the server port number during initialization

**URL:** <https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117>\
**Category:** Rails\
**Created:** [September 24, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117 "2009-09-24T23:24:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [September 24, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117/1 "2009-09-24T23:24:57Z")

</div>

In the environment.rb I need to use the port number that the server is  
being started under in an after\_initialize block in order to register  
my server with another server. I’ve looked in config  
(Rails:Configuration) and ENV, but It doesn’t look to be there. I know  
that a workaround would be to set a PORT environment variable and use  
that on the command line, for example. But I would rather use an  
already existent method.

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [September 25, 2009, 4:48am UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117/2 "2009-09-25T04:48:08Z")

</div>

Hi all,

If you think data security should be a part of Rails 3 and that  
performance with  
large data set is also important, please visit this link and cast your  
vote.

[http://rails.uservoice.com/pages/10012-rails/suggestions/327300-data-encryption-support](http://rails.uservoice.com/pages/10012-rails/suggestions/327300-data-encryption-support)

–  
Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [September 25, 2009, 10:18am UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117/3 "2009-09-25T10:18:53Z")

</div>

Hi John,

On Thu, 2009-09-24 at 10:22 -0700, johnr wrote:

> In the environment.rb I need to use the port number that the server is  
> being started under in an after\_initialize block in order to register  
> my server with another server. I’ve looked in config  
> (Rails:Configuration) and ENV, but It doesn’t look to be there. I know  
> that a workaround would be to set a PORT environment variable and use  
> that on the command line, for example. But I would rather use an  
> already existent method.

On a request-by-request basis you can test request.host\_with\_port. But,  
AFAIK, Rails is unaware of the port used by the web server until that  
server makes a request. If it were otherwise, it would be difficult to  
run a pack of mongrels in front of the app. Perhaps if you said more  
about what you mean by ‘register my server with another server’, we  
could be more help.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [September 25, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117/4 "2009-09-25T13:42:01Z")

</div>

I have one admin-like server that provides information that is used  
across a suite of other servers that provide various client-facing  
services. In order to synchronize changes to …

As I was typing, I realized that I was approaching the problem from  
the wrong direction. Instead of providing the address from the client-  
facing server, the admin-like server could get the address from the  
request. Thanks for saying just enough.

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [September 25, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/getting-the-server-port-number-during-initialization/176117/5 "2009-09-25T13:50:13Z")

</div>

On Fri, 2009-09-25 at 04:40 -0700, johnr wrote:

> Thanks for saying just enough.

You’re welcome. Glad it helped.

Best regards,  
Bill
