Duplication of context in path for a Rails app on TorqueBox with Apache ReverseProxy

爷,独闯天下 提交于 2019-12-22 12:54:42

问题


I am struggling with a scenario where I have several applications to run on a singular TorqueBox instance. When defining an app and deploying it to a root ("/") context within config/torquebox.yml the application executes properly with no apparent problems. At first I believed that this was a problem with Apache reverse proxy, but now believe it is likely an issue with Rails and/or TorqueBox, yet I can't find a clear answer or reason for the issue.

This configuration is as follows:

config/torquebox.yml contains:

web:
  context: /

Apache's httpd virtual host config for proxy is:

ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

With this I can visit http://sub.domain.tld/, with posts and posts/new routes just fine. This is by roughly following the example here (only modifications I've performed are the ones to get reverse proxy working).

But when I switch gears and run it with a context of torquebox_test_rails_production things start to get really strange. I am left to believe that there is something wrong where rails will append a duplicate copy of the context onto the path for some reason. To explain:

I change config/torquebox.yml to:

web:
  context: /torquebox_test_rails_production

and Apache's httpd virtual host conf to:

ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production/
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production/

When I do this, http://sub.domain.tld/ still loads okay, but then when I go to posts, I notice (in log/production.log) a failed GET request:

INFO -- : Started GET "/torquebox_test_rails_production/torquebox_test_rails_production/stylesheets/application.css"
FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/torquebox_test_rails_production/stylesheets/application.css"):
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) lib/rails/application.rb:144:in `call'

As you can note, it duplicates the context there on the path.

At this point the URL is still correct with simply http://sub.domain.tld/posts, but then when I click "New Post" in the scaffolded test, the app then attempts to redirect the browser to http://sub.domain.tld/torquebox_test_rails_production/posts/new. Obviously this redirection / duplication should not be occurring. Within the log I now see:

INFO -- : Started GET "/torquebox_test_rails_production/torquebox_test_rails_production/posts/new"
FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/torquebox_test_rails_production/posts/new"):
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) lib/rails/application.rb:144:in `call'

One other thing that I attempted here was to add config.relative_url_root to the config. I did this via both trying the env variable of:

RAILS_RELATIVE_URL_ROOT="/torquebox_test_rails_production"

and setting it in config/environments/production.rb to:

config.relative_url_root = "/torquebox_test_rails_production"

I would assume that this should be set itself via the context setting and therefore its purpose, but I thought it was worth a try.

I also took a cue from this question which seemed to be identical to mine, but eliminating context from torquebox.yml and just having the hostname did not solve the issue either. Instead, it seems to have eliminated the doubling of the context to path, BUT I end up with still the following error in log/production.log:

INFO -- : Started GET "/torquebox_test_rails_production/posts"
FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/torquebox_test_rails_production/posts"):
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) lib/rails/application.rb:144:in `call'

I then also attempted to define config.relative_url_root in production.rb as noted earlier with precisely the same result as just noted in terms of the error.

Any idea as to what may be happening here to provoke this behavior?


回答1:


I don't have an Apache config handy to test but is it that you have a trailing slash in your ProxyPass and ProxyPassReverse but not in your web context in your torquebox.yml?

So instead of

ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production/
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production/

try

ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production



回答2:


The solution here was that I needed to use root context ("/") with the host definition and then to remove the context out of the mod_proxy configuration.

Therefore, in the end, what worked was the following in config/torquebox.yml:

web:
  context: /
  host: sub.domain.tld

And the following in the httpd mod_proxy section for the virtual host:

<Location />
    ProxyPass http://127.0.0.1:8080/
    ProxyPassReverse http://127.0.0.1:8080/
</Location>


来源:https://stackoverflow.com/questions/24214995/duplication-of-context-in-path-for-a-rails-app-on-torquebox-with-apache-reversep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!