我有一个索引.html在民众/默认情况下应该加载,但是当我尝试访问时却收到 404 错误http://example.com/
您所查找的页面不存在。
您可能输入了错误的地址或者页面可能已移动。
这与nginx和独角兽我正在使用它来供电Rails 3
当将 unicorn 从 nginx 配置文件中取出时,问题消失并且 index.html 可以正常加载。
这是我的nginx配置文件:
upstream unicorn {
server unix:/tmp/.sock fail_timeout=0;
}
server {
server_name example.com;
root /www/example.com/current/public;
index index.html;
keepalive_timeout 5;
location / {
try_files $uri @unicorn;
}
location @unicorn {
proxy_pass http://unicorn;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
我的配置/路线.rb几乎是空的:
Advertise::Application.routes.draw do |map|
resources :users
end
这索引.html文件位于 public/index.html 并且如果我直接请求它,它可以很好地加载:http://example.com/index.html
重申一下,当我从 nginx conf 中删除所有对 unicorn 的引用时,index.html 可以毫无问题地加载,我很难理解为什么会发生这种情况,因为 nginx 应该默认尝试自行加载该文件。
--
这是来自 production.log 的错误堆栈:
Started GET "/" for 68.107.80.21 at 2010-08-08 12:06:29 -0700
Processing by HomeController#index as HTML
Completed in 1ms
ActionView::MissingTemplate (Missing template home/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:html], :locale=>[:en, :en]} in view paths
"/www/example.com/releases/20100808170224/app/views",
"/www/example.com/releases/20100808170224/vendor/plugins/paperclip/app/views",
"/www/example.com/releases/20100808170224/vendor/plugins/haml/app/views"):
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_view/paths.rb:14:in `find'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_view/lookup_context.rb:79:in `find'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_view/base.rb:186:in `find_template'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_view/render/rendering.rb:45:in `_determine_template'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_view/render/rendering.rb:23:in `render'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/haml-3.0.15/lib/haml/helpers/action_view_mods.rb:13:in `render_with_haml'
etc...
--
该虚拟主机的 nginx 错误日志为空:
2010/08/08 12:40:22 [info] 3118#0: *1 client 68.107.80.21 closed keepalive connection
我的猜测是,在 nginx 处理 index.html 请求之前,unicorn 会拦截该请求。
答案1
Rails 3 默认不提供静态资源。您必须配置 Web 服务器以提供公共服务器或添加
config.serve_static_assets = true
到您的生产环境 http://docs.heroku.com/rails3
答案2
这似乎工作。我必须编辑 nginx 配置文件:/etc/nginx/servers/appname.conf
location / {
...stuff...
# check for index.html for directory index
# if its there on the filesystem then rewite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
...other stuff..
}
使用后config.serve_static_assets = true
每秒只产生了大约一半的请求,因为我添加了这个并拥有config.serve_static_assets = false
答案3
问题在于:
try_files $uri @unicorn;
应为:
try_files $uri $uri/ @unicorn;
这也消除了使用 evil 的需要if
,并且不需要你让 Rails 提供静态文件(这很慢)。