这太奇怪了,我一直收到:
ActionController::RoutingError (No route matches "/favicon.ico")
但是我在公共目录下有favicon.ico
......有什么解决办法吗?Nginx没有抛出任何错误。
这太奇怪了,我一直收到:
ActionController::RoutingError (No route matches "/favicon.ico")
但是我在公共目录下有favicon.ico
......有什么解决办法吗?Nginx没有抛出任何错误。
运行
rake assets:precompile
然后设置
config.serve_static_assets = true
在 config\environments\production.rb
文件中修改配置,然后重启你的服务器。但我认为不需要运行 rake assets:precompile
命令。看起来 Nginx 没有处理您的静态资源(因为此静态文件请求发送到 ActionController)。检查 nginx.conf
中的公共根目录配置。以下是 Capistrano 部署的示例:
server {
listen 80;
root /var/www/my_project/current/public;
}
你的头部代码中是否使用了favicon_link_tag
帮助程序? :)
如果你想保持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 文件,即使该文件存在也会触发相同的错误。
rails new
命令生成)。 - IAmNaN删除favicon.ico前面的斜杠并尝试使用以下方式:
<link rel="shortcut icon" type="image/png" href="favicon.ico" />
RAILS_ENV=production
运行时,我遇到了同样的问题。由于我的git仓库中没有assets目录,所以我需要运行rake assets:precompile
。config.serve_static_assets = true
有效。感谢@Jiemurat。