开发者社区> 问答> 正文

Rails 3.1 Assets Pipeline 应该怎么配置?

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

展开
收起
心有灵_夕 2019-12-29 00:42:49 1073 0
1 条回答
写回答
取消 提交回答
  • 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
    )
    
    2019-12-29 00:43:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多