Rails 3显示404错误,而不是index.html(nginx +独angular兽)

我有一个index.html公共/应该加载默认情况下,而是我得到一个404错误,当我尝试访问http://example.com/

您正在查找的页面不存在。

您可能错误的地址或页面可能已经移动。

这与我正在使用Rails 3的 nginx独angular兽有关

当从nginxconfiguration文件中取出独angular兽时,问题消失,index.html加载得很好。

这是我的nginxconfiguration文件:

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; } } 

我的configuration/ routes.rb是非常空的:

 Advertise::Application.routes.draw do |map| resources :users end 

index.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 

我的猜测是独angular兽在nginx处理之前拦截对index.html的请求。

Rails 3默认不提供静态资产。 您必须configuration您的Web服务器来提供公共服务器或添加

 config.serve_static_assets = true 

到您的生产环境http://docs.heroku.com/rails3

似乎工作。 我不得不编辑nginxconfiguration文件: /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

问题在这里:

  try_files $uri @unicorn; 

这应该是:

  try_files $uri $uri/ @unicorn; 

这也消除了使用邪恶的必要性,而且并不要求你让Rails提供静态文件(这是很慢的)。