部署 部署到生产模式的问题

yfeng · 2012年02月25日 · 最后由 Lax 回复于 2018年07月19日 · 18246 次阅读

部署到生产模式后,用 rails s -e production 启动后,访问网站直接 500 错误。看 LOG 显示

Started GET "/" for 127.0.0.1 at 2012-02-24 14:33:09 +0800
Processing by IndexController#index as */*
  Rendered index/index.html.erb within layouts/application (93.0ms)
Completed 500 Internal Server Error in 608ms

ActionView::Template::Error (style.css isn't precompiled):
    2: <html>
    3: <head>
    4:   <title>www.test.com</title>
    5:   <%= stylesheet_link_tag    "style","ingo"  %>
    6:
    7:   <%= csrf_meta_tags %>
    8: </head>
  app/views/layouts/application.html.erb:5:in
`_app_views_layouts_application_html_erb___707863044_41318088'

问题是 style.css 我已经加入到 product.rb 中需要 recompile 中了啊..

在 config/environment/production.rb 中

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += ['style.css']

在生产环境中执行

~$ bundle exec rake assets:precompile

@shawnyu 我说了嘛,config.assets.precompile += ['style.css'] 我已经加了,命令 rake assets:precompile 也执行了,没报错,但是访问网站就 500 了,并且报那个错误。

public/assets 里面有 compile 后的 css 吗?

咱俩的问题有些类似http://ruby-china.org/topics/1414 替你顶一下。

有,style.css 的内容放入 application.css 中了。当然没有单独的 style.css 了

加上个.css 试试呗

<%= stylesheetlink_tag "style.css","ingo.css" %>

@piginzoo 我的文件其实就是 <%= stylesheetlink_tag "style.css","ingo.css" %> 这样写的,所以开发模式没问题.. 想问下,在生产中就用开发模式方式有什么问题?无非没有进行 CSS、JS 这些优化嘛?

在生产模式后把引用 style.css 改成 application 没报错了,难道我还需要手动改一次?

#8 楼 @yfeng 你部署的步骤是怎样的?正常的应该是这样:

  1. rake assets:precompile
  2. rails -e production

你看看 public/assets 里有没有 style.css 这个文件。

Public/assets中是有了,我把引用全换成application后没500了,但是显示出网页没样式也没图片我看路径都是 /assets/222-edgtggt.jpg 这种的 但是却找不到

从新创建了一个项目,在 index.html.erb 中放了一个 1.jpg,1.jpg 放在 app/assets/images 下面,在开发模式启动访问正常,图片出来了,通过 rake assets:precompile,1.jpg 被拷贝到了 /public/assets 下面,并且还有个 1-sdfsfsfsfsdfsd.jpg 的图片。 用 rails s -e production 启动访问,图片显示不出来,啥原因呢?

有谁知道啊?

同情 ing,我也被折磨过,看着 link 都对,就是报错,我上周那个问题解决后,总结了几步,你照着试试看:

总结一下,js 的 assets 部署步骤:(我的是 js,你的 css 大抵相同)

  1. 放置好 js 到 assets 目录,或者 vender 下(@HungYuHei 建议的对!)
  2. 在 production.rb 中加入编译它:config.assets.precompile += %w( jquery.fancybox-1.3.4.js )
  3. 在代码中加入 include-tag <%= javascript_include_tag "jquery.fancybox-1.3.4.js" %> 如果文件名中不包含“.”,就不用加坑爹的".js"后缀了。
  4. rake assets:precompile 或者 cap deploy

另外,建议你去 stackoverflow 上搜搜,我就是在上面得到提示的。

@piginzoo 你的步骤我都做了,还是不行。 抛开 js,css 不错,我新建立一个项目仅仅放一张 jpg 在 app/assets/images 下面,然后 rake assets:precompile 该图片拷贝到了/public/assets 下面 页面引用是<%=image_tag 1.jpg%> 在开发模式能显示,用 rails s -e production 后 图片不能显示,链接倒是/assets/1.jpg 哦,唉,搞不懂了..

#14 楼 @yfeng 运行几个命令,打印出来看看

cat config/environments/production.rb
rake assets:precompile
ls public/assets
cat public/assets/manifest.yml

production.rb:

Ml7Tch::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

  # Defaults to Rails.root.join("public/assets")
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  # config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
   config.assets.precompile += %w( style.css ingo.css ingo.js )

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  # config.active_record.auto_explain_threshold_in_seconds = 0.5
end

rake assets:precompile 没有报错

ls public/assets:

application-376598dc6b01c22c358813b48e8ffeaf.js
application-376598dc6b01c22c358813b48e8ffeaf.js.gz
application-380a64fb011e4afb121d4fc16c958c38.css
application-380a64fb011e4afb121d4fc16c958c38.css.gz
application.css
application.css.gz
application.js
application.js.gz
ingo-77eddf4f547c4b1e9db84bfbef2cc14a.js
ingo-77eddf4f547c4b1e9db84bfbef2cc14a.js.gz
ingo-c2060dfa713e84f751926a58bb416cae.css
ingo-c2060dfa713e84f751926a58bb416cae.css.gz
ingo.css
ingo.css.gz
ingo.js
ingo.js.gz
jstest-aa3ac1b728fbaa633fdfd9243a1c1bc8.html
jstest.html
manifest.yml
nivo-slider
rails-782b548cc1ba7f898cdad2d9eb8420d2.png
rails.png
span-e0c8cec2b154f8e9ed9c4b4328e213db.png
span.png
style-99b20730adaa724e191f1fae8f56c9b1.css
style-99b20730adaa724e191f1fae8f56c9b1.css.gz
style.css
style.css.gz
001.gif
001-d6ff44aa2840e4f4f05282bf4599310b.gif


mainifest.yml:


---
001.gif: 001-d6ff44aa2840e4f4f05282bf4599310b.gif
002.gif: 002-bc639f997930393c59f64725642b0c09.gif
003.gif: 003-1800c606f75c43be214a6e5c281c9e18.gif
004.gif: 004-0e4cbcce4459921aefefd6acbb5cc9ea.gif
005.gif: 005-ffd3a35ce21cdf3dd8b406aea1fcf0b6.gif
006.gif: 006-ca426e93b42000e5b4b9dc64aa4e8605.gif
007.gif: 007-749e5c95f6d95aae33fbebed8eb1e773.gif
008.gif: 008-3f90af7063b58a8ddb367f64e28ebc7c.gif
009.gif: 009-bc0e0731e6ce8827368f20646ad65272.gif
010.gif: 010-740fe514a21283dfe61702f66062a54a.gif
011.gif: 011-af9bd92a1498743d38cedc3bbcc1b9f2.gif
012.gif: 012-2b0806af8fd9ad4ffae206aaad4b61b9.gif
013.gif: 013-627175aff829d525bb5d0793dc345e3d.gif
014.gif: 014-97d9f63875ad1c3b495c4f61fb683f7e.gif
015.gif: 015-20e9b89c7bf1471ef1aab075331d801a.gif
016.gif: 016-3b0783c92695ad8604005cb9e610a55d.gif

rails.png: rails-782b548cc1ba7f898cdad2d9eb8420d2.png
span.png: span-e0c8cec2b154f8e9ed9c4b4328e213db.png
application.js: application-376598dc6b01c22c358813b48e8ffeaf.js
ingo.js: ingo-77eddf4f547c4b1e9db84bfbef2cc14a.js
jstest.html: jstest-aa3ac1b728fbaa633fdfd9243a1c1bc8.html
nivo-slider/demo/demo.html: nivo-slider/demo/demo-f16637c5f2a152a33ba7b722243b9637.html
nivo-slider/demo/images/nemo.jpg: nivo-slider/demo/images/nemo-29a0982d3593f5d88414871ffeacc9a7.jpg
nivo-slider/demo/images/toystory.jpg: nivo-slider/demo/images/toystory-5ae5d6ce9bfc7ee016f7f1be1d517233.jpg
nivo-slider/demo/images/up.jpg: nivo-slider/demo/images/up-213326f8b51ecafb1ca83fd2eff9a165.jpg
nivo-slider/demo/images/walle.jpg: nivo-slider/demo/images/walle-030eb031b9e5541c7cae261d41d867eb.jpg
nivo-slider/themes/default/arrows.png: nivo-slider/themes/default/arrows-704b615a2a6e7df530ba52c76c8c0f44.png
nivo-slider/themes/default/bullets.png: nivo-slider/themes/default/bullets-54f88df4746f9d2d537d8d70689c00f3.png
nivo-slider/themes/default/loading.gif: nivo-slider/themes/default/loading-2e31847824e8baa0b404a658ec39a708.gif
application.css: application-380a64fb011e4afb121d4fc16c958c38.css
ingo.css: ingo-c2060dfa713e84f751926a58bb416cae.css
style.css: style-99b20730adaa724e191f1fae8f56c9b1.css

始终提示 ActionView::Template::Error (ingo.js isn't precompiled) 在 public /assets 下已经有了 ingo.js 啊。。

@Rei 帮忙看看,谢谢

#17 楼 @yfeng 先答一个

因为有设置

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

所以生产环境的 rails s 是不处理静态文件的,页面的样式和图片都没有。要解决可以把这个选项设成 true,或者用 passenger。(真正部署是设 false)

isn't precompiled 的问题还没想出,再问下:

  1. Rails 版本是多少,是最新吗?
  2. rake assets:precompile 之后有重启 rails s 吗?

补充

顶楼是 style.css 没有,16 楼是 ingo.js 没有,16 楼对应的页面代码是什么呢?

生产环境启动的时候,是根据一次读入的 mainifest.yml 的内容生成静态文件链接的,所以要确保启动前已经 precompile 好了。

@Rei 说简单点,我 rails new aa 一个新项目,仅仅放一个 1.jpg 到 app/assets,然后在HomeController#index对应的index.html.erb中引用 <%=image_tag 1.jpg%> 在开发模式中正确,能显示,然后执行 rake assets:recompile 无错误,查看 public/assets 已经有了 1.jpg 及 1-sdfsafsdafsda.jpg 并且 public/assets/mainifest.yml中也有相关内容。然后 rails s -e production 启动 访问该页面,图片直接出布来,这是最简单的例子。

上个回复贴的内容,是我在项目中的情况,目前是 ingo.js 没有,有时候显示 application.js 这些斗报 isn't recompile 郁闷啊。

我 rails 版本是 3.2.1,rake 过后必定重启。哪位大神帮忙看看,郁闷啊,要不然生产上只有部署 develope 了。。

#20 楼 @yfeng 图片出不来是正常的,看 18 楼前半部分。

实在不行就把

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

改成 true

@Rei config.assets.compile = true 是可以显示图片了。 新建了个项目,做了一遍,发现,precompile 后,像 ingo.css 等被合并到了 application.css 中,而没有单独存在于 public/assets 中,请问这个咋整呢

#22 楼 @yfeng 把你这个项目 push 到 github 上吧,我本地跑一下

楼上代码已经改动了很多次,前后不一致了,不好说

@Rei 公司项目,唉...

#24 楼 @yfeng 我说你新建的项目

新建的很日怪,有时候有问题,有时候又没问题,当然我只是拷几个文件进去,像项目那种 JS,css 比较多,所以要出问题,今天把项目重新建,但是确实 css 没进 public/assets.明天再试试..我要疯了..

把资源丢到/public 下去了,不折腾了

@yfeng , 我想问问 rails3.2 中把图片放到/public 目录下面,image_tag 不能显示,貌似 image_tag 只能显示 app/assets/images/下的静态资源图片,怎么解决呢?

@yfeng ,我的 tutor 就是告诉我不能把图片放在 app/assets/images 中,要不最后就会遇到你上面描述的问题,可是我把图片放在/public 目录下,那在 view 中怎么显示呢?

@yfeng assets-pipeline 是一套很诡异的东西,建议去看看官方教程,基本的理念是 app/assets 中的东西会从管道(pipeline,实际比较复杂。。)编译到 public/assets 下变成静态的(减少请求),而且三个文件夹能够保持路径的相对性,比如在 stylesheet 里找一个根目录下的图片会自动从 images 根目录里找,将 style.css 放到预编译队列应该是没问题的(检查 public/assets 下有无 style.css)。在产品环境下所有的 asset 都需要编译,当然你可以在在 product.rb 中设置使得可以伺服一般的静态文件

我也遇到这个问题了,最后设置了 config.assets.compile = true 但是 public 目录下不需要编译存在编译文件也可以使用生产模式访问。 以后有谁看到了这个,问题解决了一定要将答案返回到这里呀

迫不得已也设置了config.assets.compile = true

Did anybody fix it?

又一个掉坑里的人,使用 kindeditor 编辑器,开发模式上传图片啥的都没问题,换到生产模式,同样的路径,就访问不了,折腾了 2 天了,一点头绪没😅

2018 年,我又来了😅

ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.)

执行 precompile 时加上部署环境的环境变量 RAILS_ENV=production 之后,就可以了,其它设置保持默认。

bundle exec rake assets:precompile RAILS_ENV=production
需要 登录 后方可回复, 如果你还没有账号请 注册新账号