Nginx
Rails 3 顯示 404 錯誤而不是 index.html (nginx + unicorn)
我在public/中有一個****index.html應該預設載入,但是當我嘗試訪問http://example.com/時卻收到 404 錯誤
您要查找的頁面不存在。
您可能輸入了錯誤的地址或頁面可能已移動。
這與我用來為Rails 3供電的****nginx和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; } }
我的config/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
我的猜測是 unicorn 在 nginx 處理它之前攔截了對 index.html 的請求。
Rails 3 預設不提供靜態資源。您必須配置您的 Web 伺服器以提供公共伺服器或添加
config.serve_static_assets = true
到您的生產環境 http://docs.heroku.com/rails3
這似乎有效。我不得不編輯 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