Forum: Ruby on Rails Getting the true path behind the proxy

3b8ad006781b1a3ab36dc76e2a05ce9c?d=identicon&s=25 Cris Shupp (cshupp)
on 2016-09-03 00:34
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?


3b8ad006781b1a3ab36dc76e2a05ce9c?d=identicon&s=25 Cris Shupp (cshupp)
on 2016-09-06 18:14
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:
module ServletSupport

  SERVLET_REQUEST = 'java.servlet_request'

  def servlet_response

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

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

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


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 +
    $log.debug("Non proxy path is #{path}")

Sample invocation:
non_proxy_url(path_string: root_path)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.