Rails,找不到favicon.ico文件

13

这太奇怪了,我一直收到:

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

但是我在公共目录下有favicon.ico......有什么解决办法吗?Nginx没有抛出任何错误。


1
没有查看nginx.conf文件很难说。 - Roman
2
请仔细检查文件是否位于正确的项目公共目录中。尝试重新启动nginx和rails应用程序。如果在html/layout中使用了一些HTML来设置网站图标,请也检查一下它。 - Zabba
6个回答

12

运行

rake assets:precompile

然后设置

config.serve_static_assets = true
config\environments\production.rb 文件中修改配置,然后重启你的服务器。但我认为不需要运行 rake assets:precompile 命令。

5
serve_static_assets 对我很关键。 - Mark Fraser

11

看起来 Nginx 没有处理您的静态资源(因为此静态文件请求发送到 ActionController)。检查 nginx.conf 中的公共根目录配置。以下是 Capistrano 部署的示例:

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

你的头部代码中是否使用了favicon_link_tag帮助程序? :)


11

如果你想保持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;
      }
}

5

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


1
如果您从twitter-bootstrap-rails gem运行install rake任务,它将在您的应用程序布局文件中放置一个链接引用到favicon.ico,该文件默认为空文件(由rails new命令生成)。 - IAmNaN

1

删除favicon.ico前面的斜杠并尝试使用以下方式:

<link rel="shortcut icon" type="image/png" href="favicon.ico" />

1
当我第一次从git仓库克隆代码并使用RAILS_ENV=production运行时,我遇到了同样的问题。由于我的git仓库中没有assets目录,所以我需要运行rake assets:precompile
另外,我使用rails s运行,所以config.serve_static_assets = true有效。感谢@Jiemurat。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接