Rails 3.1无法提供favicon.ico文件

3
尽管位于“/public”目录中,但如果我访问“http://site.example.com/favicon.ico”,会出现404页面。有趣的是,如果我尝试访问“http://site.example.com/500.html”,我也会得到404页面,这使我相信根本没有提供“/public”文件。我正在使用Nginx和Unicorn。是否有Rails设置可以禁用提供“/public”资产的功能? 编辑 我的nginx配置:
server {
  listen 80;
  client_max_body_size 4G;
  server_name _;

  keepalive_timeout 5;

  # Location of our static files
  location ~ ^/(assets)/  {
    root /srv/ctr/current/public;
    gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header  Cache-Control public;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    # If you don't find the filename in the static files
    # Then request it from the unicorn server
    if (!-f $request_filename) {
      proxy_pass http://app_server;
      break;
    }
  }

  # error_page 500 502 503 504 /500.html;
  # location = /500.html {
  #   root /var/rails/testapp/public;
  # }
}

我在路由中有root :to => 'reports#index',但我不知道它会有什么区别。

解决方法 我将root /srv/ctr/current/public;这一行移到了keepalive_timeout 5;上面。


我无法确定Rails 3.1,Nginx和Unicorn是否忽略我的公共目录中的favicon、404.html或500.html。这里是我的配置,以检查是否有任何差异:https://gist.github.com/1589113 祝你好运 :) - Tim Brandes
2个回答

1
请检查您的routes.rb文件,确保没有类似于此的行:

root :to => "home#index"

同时检查Nginx.conf文件,确保您已经

root /path/to/app/public;

适用于您的服务器/虚拟主机。

Dave


2
Dave,你确定 root :to => "home#index" 是这个错误的来源吗?我有这个声明,它可以获取我的favicon等。但是 root /path/to/app/public; 指令也是我的建议。 - Tim Brandes
1
timbrandes,刚刚检查了一下,即使在favicon中加入路由等也应该可以工作。由于我们的项目具有多许可证和多公司的特点,所以已经有一个指令根据使用的主机名来获取favicon(别问我为什么!!)。 - detheridge02
1
@detheridge02 你可能需要编辑一下你回答的第一部分,因为它被接受了,这可能会误导那些不读评论的用户! - asymmetric

0

Rails,无法找到 favicon.ico

配置你的 nginx .conf 文件

vim /etc/nginx/conf.d/your_project.conf

server {
    ......

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

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