Rails,favicon.ico未找到

JP *_*shy 13 ruby-on-rails

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

ActionController::RoutingError (No route matches "/favicon.ico")
Run Code Online (Sandbox Code Playgroud)

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

Jie*_*rat 12

rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

然后设定

config.serve_static_assets = true
Run Code Online (Sandbox Code Playgroud)

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

  • serve_static_assets是我的关键. (5认同)

Vol*_*ldy 11

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

server {
  listen       80;
  root /var/www/my_project/current/public;
}
Run Code Online (Sandbox Code Playgroud)

favicon_link_tag在头脑中使用帮手:)?


Ove*_*d D 10

如果你想保持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;
      }
}
Run Code Online (Sandbox Code Playgroud)


Abe*_*ker 5

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