Getting the true path behind the proxy


#1

Our rails application is now being tested behind Apache and mod_proxy.

When I use the various _url methods to get a needed route rails gives
the path to the proxying server. Obviously, in 99% of the cases, this
is usually what we want. I happen to be in that 1%…

How can I get rails to give me the URL relative the actual host and the
host’s port?

Thanks,

Cris


#2

I have no clue how to do this pure rails, but, as I am in the land of
JRuby/Java, here is what I did:

Created a controller concern called:
/app/controllers/concerns/servlet_support.rb

module ServletSupport

SERVLET_REQUEST = ‘java.servlet_request’

def servlet_response
request.headers[SERVLET_REQUEST]
end

#returns the host beneath the proxy
def true_address
addr = servlet_response.getLocalName
$log.debug(“True address/hostname is #{addr}”)
addr
end

#returns the port beneath the proxy
def true_port
port = servlet_response.getLocalPort
$log.debug(“True port is #{port}”)
port
end

#returns the scheme beneath the proxy
def secure?
s = servlet_response.isSecure
$log.debug(“is https? #{s}”)
s
end

end

servlet_response returns a javax.servlet.ServletRequest

In my application controller I added:

def non_proxy_url(path_string:)
path_string = ‘/’ + path_string unless path_string.start_with? ‘/’
scheme = secure? ? ‘https’ : ‘http’
path = scheme +
‘://’+true_address.to_s+’:’+true_port.to_s+path_string
$log.debug(“Non proxy path is #{path}”)
path

Sample invocation:
non_proxy_url(path_string: root_path)