Server IP

Can a ruby on rails app obtain the ip of the server it is running on?

I’d like the application to be able to determine x set of data is for y
data, for example:

These domains all point to the same ip…
mydomain-1.com
mydomain-2.com
mydomain-3.org

I can obtain the domain name from the request by using:
@request.domain

I’d like the application to grab the corresponding ip for that url.
Someone had previously suggested using @request.remote_ip, but that
obtains the ip from the requesting address, not the address of the url.

I’m thinking of using this method:
domain_name = @request.domain
ipaddr = IPSocket.getaddress( domain_name )

Is this my best bet?

why not use virtual hosting instead, wouldn’t that be much easier?

“Joe” == Joe C. [email protected] writes:

Can a ruby on rails app obtain the ip of the server it is running on?

Generally speaking no, it can’t, because there is no such thing as
the IP address of a machine. Even my lowly iMac has two, one for the
Ethernet port and one for the wireless. The main webserver at work has
eight IP adresses, none of which is more important than the others.

I’d like the application to grab the corresponding ip for that url.
Someone had previously suggested using @request.remote_ip, but that
obtains the ip from the requesting address, not the address of the url.

Why do you want that? And why do you even need to figure it out? It’s
your application running on your webserver, why don’t you already
know which IPs it’s listening on?

I’m thinking of using this method:
domain_name = @request.domain
ipaddr = IPSocket.getaddress( domain_name )

Is this my best bet?

Almost certainly not. If they’re your domains, you already have this
information and you’re better off just putting it into a hash for
lookup. If they’re not your domains, you can’t trust that there will
be only one IP for each name.

This whole question looks a whole lot like you have a problem A, and
you think a solution is B. So you ask how to do B. But in reality
there is probably an easier solution C, and B is just a wild goose
chase. You’d probably get more useful answers if you asked about A
directly instead.

	     Calle D. <[email protected]>
	 http://www.livejournal.com/users/cdybedahl/
 "I'd rather hang on to madness than normality" -- KaTe Bush

Calle D. wrote:

Why do you want that? And why do you even need to figure it out? It’s
your application running on your webserver, why don’t you already
know which IPs it’s listening on?
Actually, there is a situation where you might want to discover the
public address of the site. I’ve just been working on a CMS, and one of
the problems we had with a bunch of imported data was links that worked
under the old system now not pointing to anything, or pointing to
offsite addresses that aren’t valid. The client wanted a way to have
the site scan itself for dead links, and report the results.

The only way to do this reliably turned out to be spawning a thread to
do HTTP GETs of each link it could find in the content. In order to do
that for internal, relative links, you need to know your own host
address.

I got around the problem by having an external config file supply the
information, outside version control. That way each developer can test
the code against their own install and not conflict. The scan is
triggered from a RailsCron job in its own thread, so calling back into
the same server process isn’t a problem.

I can see why there’s not really an easy way around this issue (apart
from anything else, if the server process is FCGI, then it doesn’t even
know which port it’s on, much less what its public hostname might be),
but it’s rather inconvenient for this particular problem.

“Alex” == Alex Y. [email protected] writes:

Calle D. wrote:

It’s your application running on your webserver, why don’t you
already know which IPs it’s listening on?

Actually, there is a situation where you might want to discover the
public address of the site.
[snip]
I got around the problem by having an external config file supply the
information, outside version control.

That’s exactly what I meant, actually. I’m not trying to claim that an
application never needs to know what IP it’s listening on, because, as
you point out, sometimes it does. What I’m saying is that if it needs
to know this, the site administrators should tell it (as you do with
the mentioned config file). The application should not resort to silly
tricks with DNS or ifconfig to try and guess where it lives, because
that will be unreliable and brittle.

	     Calle D. <[email protected]>
	 http://www.livejournal.com/users/cdybedahl/

“Run little fishies! Run like the wind!” – Steve McAndrewSmith,
A.S.R