zero_padded
zero_padded

Reputation: 133

How to get the HTTP_HOST from Rails?

I need to set some server specific variables in a rails application that will run on at least two different servers. What is the best way to get the request's HTTP_HOST value in order to know what is the current server and set these variables accordingly? I'm using Apache 2 with Passenger.

Upvotes: 4

Views: 5389

Answers (2)

hgmnz
hgmnz

Reputation: 13306

Think you're looking for request.env["SERVER_ADDR"].

Upvotes: 10

andersonbd1
andersonbd1

Reputation: 5416

Are the 2 servers involved in load balancing? If so, you won't want to grab the host from the request, because it'll be the same in both cases. You'll want to grab it in some generic ruby call. It's been a while since I've done Ruby, but probably something like (untested - rough idea):

Resolv::Hosts.getNames(Resolv::Hosts.getAddress('localhost'))

Upvotes: 0

Related Questions