尽管在/public
目录中,如果我访问http://site.example.com/favicon.ico
,我会收到404页面。有趣的是,如果我尝试访问http://site.example.com/500.html
,我也会收到404页面,让我相信/public
文件根本没有提供。我和Unicorn一起运行Nginx。 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;
答案 0 :(得分:1)
检查你的routes.rb以确保你没有像
这样的行root :to => "home#index"
同时检查Nginx.conf以确保您有
root /path/to/app/public;
用于您的服务器/虚拟主机。
戴夫
答案 1 :(得分:0)
配置你的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;
}
}