Rails,favicon.ico未找到

这很奇怪,我一直在接受:

ActionController::RoutingError (No route matches "/favicon.ico") 

但我的公共目录中有favicon.ico …任何想法如何解决这个问题? Nginx根本不会抛出错误。

似乎nginx不处理你的静态资产(因为这个静态文件请求转到ActionController)。 检查nginx配置文件nginx.conf公共根目录。 以下是Capistrano部署的示例:

 server { listen 80; root /var/www/my_project/current/public; } 

你在头脑中使用了favicon_link_tag助手吗?

 rake assets:precompile 

然后设定

 config.serve_static_assets = true 

config\environments\production.rb文件中。 然后重启服务器。 但我认为rake assets:precompile不需要rake assets:precompile

如果你想保持config.serve_static_assets = false,如果你有nginx或apache,那么你可以告诉nginx直接静态地提供文件。 出于性能原因,这一点尤为重要,因为您不希望rails提供这些资产。

下面是一个示例,它也正确地将nginx静态地提供给assets目录:

 server { listen 80; root /var/www/my_project/current/public; location / { proxy_pass http://mysite; proxy_redirect off; proxy_set_header X_FORWARDED_PROTO https; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # static resource routing - both assets folder and favicon.ico location ~* ^/assets/|favicon.ico { # Per RFC2616 - 1 year maximum expiry # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html expires 1y; add_header Cache-Control public; # Some browsers still send conditional-GET requests if there's a # Last-Modified header or an ETag header even if they haven't # reached the expiry date sent in the Expires header. add_header Last-Modified ""; add_header ETag ""; break; } } 

确保favicon.ico文件不为空(字节大小> 0)。 出于某种原因,我有一个空的favicon.ico文件,它触发了相同的错误,即使该文件确实存在。

在favicon.ico之前删除斜杠符号并尝试使用以下内容:

  

当我第一次从git存储库克隆代码并使用RAILS_ENV=production运行时,我遇到了同样的问题。 由于我的git存储库中没有assets目录,我需要运行rake assets:precompile

我也使用rails运行,所以config.serve_static_assets = true工作。 谢谢@Jiemurat