我正在努力解决三个主要问题,我感谢对其中任何一个的帮助。
1) 如何将 Rails 应用程序配置为具有 myurl.com/myapp/
作为根?
我试过 routes.rb
:
scope '/myapp' || '/' do
# all resources and routes go here
root :to => "papers#index"
resources :papers
end
并在
environment.rb
,我把这个加到了顶部ENV['RAILS_RELATIVE_URL_ROOT'] = "/myapp"
这几乎有效,除了
rake routes
不会为“/”打印任何路由,并且 GET myurl.com/myapp/
生产 ActionController::RoutingError (No route matches [GET] "/")
2)它需要告诉apache什么?
我的共享服务器的提供者建议把这个放到
~/html/.htaccess
RewriteEngine on
RewriteRule ^myapp/(.*)$ /fcgi-bin/rails4/$1 [QSA,L]
与
/fcgi-bin/rails4
存在#!/bin/sh
# This is needed to find gems installed with --user-install
export HOME=/home/kadrian
# Include our profile to include the right RUBY
. $HOME/.bash_profile
# This makes Rails/Rack think we're running under FastCGI. WTF?!
# See ~/.gem/ruby/1.9.1/gems/rack-1.2.1/lib/rack/handler.rb
export PHP_FCGI_CHILDREN=1
# Get into the project directory and start the Rails server
cd $HOME/rails4
exec bundle exec rails server -e production
当我点击网站上的任何链接时,浏览器网址会发生变化,例如至
myurl.com/fcgi-bin/rails4/papers/1
,它应该在哪里myurl.com/myapp/papers/1
.我怎样才能防止这种情况?3) 如何让 Assets 工作
我觉得这会以某种方式与 1) 和 2) 一起解决。但是,现在,该应用程序尝试执行以下操作:
GET myurl.com/assets/application-5e86fb668d97d38d6994ac8e9c45d45e.css
产生
404 Not Found
. Assets 也应该在子目录下,对吗?我如何告诉 Rails 在那里放置/找到它们?
最佳答案
为了尝试回答您的问题,我将做出一些假设。
myurl.com
上有一个静态站点。并且您只想在 myurl.com/myapp
上提供 Rails 应用程序基于这些假设,我相信如果您:
~/html/myapp
下文件夹 .htaccess
文件到 ~/html/myapp/public
内容:AddHandler fastcgi-script .fcgi
选项 +FollowSymLinks +ExecCGI
重写引擎开启
RewriteCond %{REQUEST_FILENAME} !-f
重写规则 ^(.*)$ dispatch.fcgi/$1 [QSA,L]
ErrorDocument 500“Rails 应用程序无法正常启动”
dispatch.fcgi
文件到 ~/html/myapp/public
内容:ENV['RAILS_ENV'] ||= '生产'
ENV['GEM_HOME'] = File.expand_path('your_gem_home')
需要'fcgi'
需要 File.join(File.dirname(__FILE__), '../config/environment.rb')
chmod +x ~/html/myapp/public/dispatch.fcgi
该应用程序应该可以正常启动并且路由也很好......
我认为您无需担心设置
config.action_controller.relative_url_root
.我也会使用
bundle install --deployment
安装您的 gems在部署您的应用程序之前。最后,您没有获得根路由的原因是因为没有:
root :to => 'controller#action'
在您的 config/routes.rb
希望这会有所帮助,如果没有,请查看:
Dreamhost - Rails 3 and FastCGI和 FastCGI setup其中包括一些关于使用
ENV['RAILS_RELATIVE_URL_ROOT']
的提示
关于ruby-on-rails - 在 Apache 下的子目录中为生产配置 Rails 4 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047261/