JP Silvashy
JP Silvashy

Reputation: 48525

Rails, favicon.ico not found

This is soo odd, I've been receiving:

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

but I have the favicon.ico in my public directory... any ideas how to solve this? Nginx doesn't throw an error at all.

Upvotes: 13

Views: 16465

Answers (6)

Bruce
Bruce

Reputation: 127

I was facing the same problem when I first clone code from git repository and run with RAILS_ENV=production. Since there was no assets directory in my git repository, I needed to run rake assets:precompile.

Also I was run with rails s, so config.serve_static_assets = true worked. Thanks @Jiemurat

Upvotes: 1

Oved D
Oved D

Reputation: 7442

If you want to keep config.serve_static_assets = false, which is recommended if you have nginx or apache, you can tell nginx to statically serve the files directly. This is especially important for performance reasons, as you don't want rails serving these assets.

Below is a sample which also correctly has nginx statically serve the assets directory:

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

Upvotes: 11

Jiemurat
Jiemurat

Reputation: 1629

Run

rake assets:precompile

then set

config.serve_static_assets = true

in config\environments\production.rb file. Then restart your server. But I think rake assets:precompile is not required.

Upvotes: 12

Abe Voelker
Abe Voelker

Reputation: 31574

Make sure that the favicon.ico file isn't empty (byte size > 0). For some reason I had an empty favicon.ico file which was triggering the same error, even though the file did exist.

Upvotes: 5

Voldy
Voldy

Reputation: 12868

It seems that nginx doesn't handle your static assets (since this request for static file goes to the ActionController). Check public root in nginx config file nginx.conf. Here is an example with Capistrano deployments:

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

And do you use a favicon_link_tag helper in your head :) ?

Upvotes: 11

lkahtz
lkahtz

Reputation: 4796

delete slash sign before favicon.ico and try to use something like:

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

Upvotes: 1

Related Questions