js/css文件加载不上,求解
环境:
rails 3.1.3
unicorn 4.1.1
nginx 1.0.6
现象: 用rails s启动开发模式,一切正常。请求地址为http://dev:3000/assets/application.js?body=1 用rails s -e production 启动,assets pipeline异常。请求地址为http://dev:3000/javascripts/application.js404错误, 用unicorn -c config/unicorn.rb 启动,异常。请求地址为http://dev/assets/application.js?body=1, 404错误 用unicorn -c config/unicorn.rb -E production启动,异常。请求地址为:http://dev/javascripts/application.js, 404错误。且仅加载application.js/css,上面3种情况则会加载很多其他的js/css 404时, nginx的日志为:
open() "xxxxx/public/assets/application.js" failed (2: No such file or directory
rails的日志为:
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
unicorn的日志为:
cache: [GET /javascripts/application.js] miss production配置(局部,其他选项默认)为:
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.assets.debug = true
config.assets.compile = false # 表示rails不会即时编译asset ls public/assets # 不存在,表示你没有运行 bundle exec rake assets:precompile config.serve_static_assets = false # 表示unicorn不处理静态文件 配置nginx,让它来处理静态文件,在server节点增加:
# if the request is for a static resource, nginx should serve it directly
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
# this probably needs some work with Rails 3.1's asset pipe_line
location ~ ^/(assets|images|javascripts|stylesheets|system)/ {
root /u/apps/your_project_name/current/public;
expires max;
break;
}
默认情况下,rails会编译application.js和application.css(作为入口),同时编译被require指令引用的文件 如果还有其他文件需要编译,则需要修改 config/environments/products.rb,例如:
config.assets.precompile += %w(
active_admin.js active_admin.css
)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。