ruby-on-rails – 为什么我需要在应用服务器而不是Web服务器上预编译Rails资产?

ruby-on-rails – 为什么我需要在应用服务器而不是Web服务器上预编译Rails资产?,第1张

概述我正在为这个小型的ubuntu precision64服务器设置部署一个简单的Rails应用程序: * 1运行nginx的Web服务器 * 2个运行独角兽的应用服务器 * 1 db服务器运行 postgresql 我的服务器配置了Puppet,我正在用capistrano推送应用程序. 我的Capfile load 'deploy'# Uncomment if you are using Rai 我正在为这个小型的ubuntu precision64服务器设置部署一个简单的Rails应用程序:
* 1运行Nginx的Web服务器
* 2个运行独角兽的应用服务器
* 1 db服务器运行 postgresql

我的服务器配置了Puppet,我正在用cAPIstrano推送应用程序.

我的Capfile

load 'deploy'# Uncomment if you are using Rails' asset pipelineload 'deploy/assets'load 'config/deploy' # remove this line to skip loading any of the default tasks

deploy.rb文件非常简单

# Execute "bundle install" after deploy,but only when really neededrequire "bundler/cAPIstrano"# name of the application in scm (GIT)set :application,"devops-test-app"set :repository,"https://github.com/geoffroymontel/devops-test-app.git"set :scm,:gitset :deploy_to,"/var/www/#{application}"# server there the web server is running (Nginx)role :web,"172.16.0.2"# server there the db is running# This is where Rails migrations will runrole :db,"172.16.0.3",:primary => true# servers there the app servers are running (unicorn)role :app,"172.16.0.4","172.16.0.5"set :rails_env,:production# user on the serverset :user,"deployer"set :use_sudo,falsenamespace :deploy do   task :start,:roles => :app,:except => { :no_release => true } do    run "service unicorn_#{application} start"  end  task :stop,:except => { :no_release => true } do    run "service unicorn_#{application} stop"  end  task :restart,:except => { :no_release => true } do    run "service unicorn_#{application} restart"  end  task :copy_in_database_yml do    run "cp #{shared_path}/config/database.yml #{latest_release}/config/"  end  # Precompile assets  # I have to precompile the assets on the app servers too,and I don't really kNow why...  # namespace :assets do  #   task :precompile,:roles => [:web,:app],:except => { :no_release => true } do  #     run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}  #   end  # endendbefore "deploy:assets:precompile","deploy:copy_in_database_yml"

如果我不在应用服务器上预编资产,则应用程序将失败.

$cap RolES="app" COMMAND="cat /var/www/devops-test-app/current/log/production.log" invoke  * 2013-05-01 21:43:10 executing `invoke'  * executing "cat /var/www/devops-test-app/current/log/production.log"    servers: ["172.16.0.4","172.16.0.5"]    [172.16.0.5] executing command ** [out :: 172.16.0.5] Connecting to database specifIEd by database.yml ** [out :: 172.16.0.5] Connecting to database specifIEd by database.yml ** [out :: 172.16.0.5] Started GET "/posts" for 172.16.0.2 at 2013-05-01 19:42:10 +0000 ** [out :: 172.16.0.5] Processing by postsController#index as HTML ** [out :: 172.16.0.5] Rendered posts/index.HTML.erb within layouts/application (25.7ms) ** [out :: 172.16.0.5] Completed 500 Internal Server Error in 122ms ** [out :: 172.16.0.5]  ** [out :: 172.16.0.5] ActionVIEw::Template::Error (application.CSS isn't @R_310_3013@): ** [out :: 172.16.0.5] 2: <HTML> ** [out :: 172.16.0.5] 3: <head> ** [out :: 172.16.0.5] 4:   <Title>DevopsTestApp</Title> ** [out :: 172.16.0.5] 5:   <%= stylesheet_link_tag    "application",:media => "all" %> ** [out :: 172.16.0.5] 6:   <%= JavaScript_include_tag "application" %> ** [out :: 172.16.0.5] 7:   <%= csrf_Meta_Tags %> ** [out :: 172.16.0.5] 8: </head> ** [out :: 172.16.0.5] app/vIEws/layouts/application.HTML.erb:5:in `_app_vIEws_layouts_application_HTML_erb__677166568443748084_17536100' ** [out :: 172.16.0.5] app/controllers/posts_controller.rb:7:in `index'

如果我取消注释deploy.rb中的注释行,一切都很好.

但为什么 ??
我认为您只需要在Web服务器上编译资产,而不是在应用服务器上编译资产.

请帮我理解为什么:)

谢谢,最好的问候

若弗鲁瓦

解决方法 因为,除其他外,Rails会生成预编译资产的清单,包括文件名中文件的哈希值,然后在页面中包含资源时使用这些名称.当你说image_url(‘foo.jpg’)时,Rails最终会在源代码中生成foo-b48cf0140bea12734db05ebcdb012f1d265bed84.jpg.

Rails需要知道要为这些资产使用的编译名称,因此它必须具有清单,因此预编译需要在应用服务器上完成.

看一下public / assets / manifest.yml – 这是Rails需要的文件,以便正确地提供预编译资产.

总结

以上是内存溢出为你收集整理的ruby-on-rails – 为什么我需要在应用服务器而不是Web服务器上预编译Rails资产?全部内容,希望文章能够帮你解决ruby-on-rails – 为什么我需要在应用服务器而不是Web服务器上预编译Rails资产?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1290551.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存