我正在努力解决这样的情况:我有多个应用程序要在单个 TorqueBox 实例上运行。当定义应用程序并将其部署到 config/torquebox.yml 中的根(“/”)上下文时,应用程序可以正常执行,没有明显的问题。起初我认为这是 Apache 反向代理的问题,但现在认为这可能是 Rails 和/或 TorqueBox 的问题,但我找不到该问题的明确答案或原因。
此配置如下:
config/torquebox.yml
包含:
web:
context: /
Apache 的 httpd 代理虚拟主机配置是:
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
有了这个,我可以访问 http://sub.domain.tld/
,使用 posts
和 posts/new
路由就可以了。这是大致遵循the example here (只有我执行的修改才能使反向代理正常工作)。
但是当我切换齿轮并在 torquebox_test_rails_Production
上下文中运行它时,事情开始变得非常奇怪。我相信有一些错误,rails 会出于某种原因将上下文的重复副本附加到路径上。解释一下:
我将config/torquebox.yml
更改为:
web:
context: /torquebox_test_rails_production
和 Apache 的 httpd 虚拟主机配置为:
ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production/
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production/
当我这样做时,http://sub.domain.tld/
仍然可以正常加载,但是当我转到 posts
时,我注意到(在 >log/Production.log
) 失败的 GET 请求:
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'
正如您所注意到的,它复制了路径上的上下文。
此时,URL 仍然正确,只需 http://sub.domain.tld/posts
,但是当我在脚手架测试中单击“新帖子”时,应用程序会尝试将浏览器重定向到 http://sub.domain.tld/torquebox_test_rails_product/posts/new
。显然这种重定向/重复不应该发生。在日志中我现在看到:
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'
我在这里尝试的另一件事是将 config.relative_url_root
添加到配置中。我通过尝试以下环境变量来做到这一点:
RAILS_RELATIVE_URL_ROOT="/torquebox_test_rails_production"
并在config/environments/Production.rb
中将其设置为:
config.relative_url_root = "/torquebox_test_rails_production"
我认为这应该通过上下文设置及其目的来设置,但我认为值得一试。
我还从 this question 中得到了提示这似乎与我的相同,但是从 torquebox.yml
中删除 context
并且仅拥有主机名也无法解决问题。相反,它似乎消除了路径上下文的加倍,但我最终在 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'
然后,我还尝试在 production.rb
中定义 config.relative_url_root
(如前所述),其结果与刚刚在错误中指出的结果完全相同。
你知道这里可能发生什么会引发这种行为吗?
最佳答案
我没有方便测试的 Apache 配置,但是您的 ProxyPass 和 ProxyPassReverse 中有一个尾部斜杠,但在您的 Torquebox.yml 的 Web 上下文中却没有吗?
所以而不是
ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production/
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production/
尝试
ProxyPass / http://127.0.0.1:8080/torquebox_test_rails_production
ProxyPassReverse / http://127.0.0.1:8080/torquebox_test_rails_production
关于ruby-on-rails - 使用 Apache ReverseProxy 复制 TorqueBox 上 Rails 应用程序路径中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214995/