简体   繁体   English

在apache反向代理后面的子目录中的Rails

[英]Rails in a subdirectory behind apache reverse proxy

I don't get it. 我不明白。 I tried to run a Rails app behind an apache reverse proxy. 我试图在apache反向代理后面运行一个Rails应用程序。 I'm using Unicorn on port 8080. 我在端口8080上使用Unicorn。

bundle exec unicorn -c config/unicorn.rb -E production -p 8080

Apache VirtualHost Apache VirtualHost

ProxyPass /foo/ http://localhost:8080/
ProxyPassReverse /foo/ http://localhost:8080/

This basically works. 这基本上有效。 A request to http://domain.tld/foo/ arrives at the Rails app. http://domain.tld/foo/的请求到达Rails应用程序。 What follows is a redirect to an authentication mechanism using the following in ApplicationController.before_filter : 以下是使用ApplicationController.before_filter的以下内容重定向到身份验证机制:

redirect_to controller: 'sessions', action: 'index'

As expected, I will be redirected to http://domain.tld/sessions/ . 正如所料,我将被重定向到http://domain.tld/sessions/ Now I'd like to configure Rails to redirect to http://domain.tld/foo/sessions/ globally, without explicitly mentioning it with every redirect. 现在我想将Rails配置为全局重定向到http://domain.tld/foo/sessions/ ,而不是每次重定向都明确提及它。

I tried using this in config/environments/production.rb: 我尝试在config / environments / production.rb中使用它:

config.relative_url_root = '/foo'
config.action_controller.relative_url_root = '/foo'

And starting Unicorn with this: 并用这个开始Unicorn:

RAILS_RELATIVE_URL_ROOT='/foo' bundle exec unicorn -c config/unicorn.rb -E production -p 8080

Unfortunately, this does not work. 不幸的是,这不起作用。 It doesn't change the behaviour at all. 它根本不会改变行为。 I've added debug output before the redirect to see, what's going on. 我在重定向之前添加了调试输出,看看是怎么回事。

puts Rails.application.config.relative_url_root
puts ENV['RAILS_RELATIVE_URL_ROOT']
puts url_for controller: 'sessions', action: 'index'

This prints out: 打印出:

/foo
/foo
http://domain.tld/sessions

Can anybody tell me why Rails does not take the configuration into account? 任何人都可以告诉我为什么Rails不考虑配置?

Looks like I've got the solution. 看起来我已经有了解决方案。 I had to change the apache config to this 我不得不将apache配置更改为此

ProxyPass /foo/ http://localhost:8080/foo/
ProxyPassReverse /foo/ http://localhost:8080/foo/

and config.ru to this 和config.ru到这个

if Rails.env.production?
  map '/foo' do
    run Rails.application
  end
else
  run Rails.application
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM