新手问题 使用 Unicorn 部署 production 与直接使用 rails s -e production 生成的 assets 目录不同该怎么设置?

dothide · December 17, 2013 · Last by dothide replied at December 19, 2013 · 5598 hits

大家好,最近学习 ror 遇到些问题,经过阅读官方文档和 google 还是没能完全理解,因此在这里发帖求助,主要问题如下:

我的部署环境是:nginx+unicorn

其中 unicorn 是使用 init 脚本管理的,我的脚本如下:

#! /bin/sh
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/datas/ruby/dothide
APP_USER=root
PID=$APP_ROOT/tmp/pids/unicorn.pid
ENV=production
CMD="bundle exec unicorn_rails -E $ENV -D -c $APP_ROOT/config/unicorn.rb"
action="$1"
set -u

...

网站目录下 config/unicorn.rb 如下:

module Rails
  class <<self
    def root
      File.expand_path(__FILE__).split('/')[0..-3].join('/')
    end
  end
end
rails_env = ENV["RAILS_ENV"] || "production"

preload_app true
working_directory Rails.root
pid "#{Rails.root}/tmp/pids/unicorn.pid"
stderr_path "#{Rails.root}/log/unicorn.log"
stdout_path "#{Rails.root}/log/unicorn.log"

listen 5000, :tcp_nopush => false

listen "/tmp/unicorn.smdp.sock"
worker_processes 6
timeout 120

if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

before_fork do |server, worker|
  old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      puts "Send 'QUIT' signal to unicorn error!"
    end
  end
end

根据官方文档 assets pipeline 我已经配置好了其他的参数,并且使用以下命令生成的 html 代码是正常的 application-MD5.js 形式,访问地址为 localhost:3000

rails s -e production

但是我部署的 nginx+unicorn 却不能生成以上形式的 js 及 css 文件,仍然是默认的/javascript/application.js 和/stylesheets/application.css

非常不解啊...

运行 RAILS_ENV=production bundle exec rake assets:precompile

应该是 ls 说的 另外,可以考虑这种静态资源用 nginx 处理,不用发送到 unicorn

#1 楼 @WolfLee #2 楼 @mahone3297 感谢楼上两位,经过多次尝试,并参考了 Heroku 的部署顺序,问题已经解决拉

特别写了个帖子记录: http://ruby-china.org/topics/16308

You need to Sign in before reply, if you don't have an account, please Sign up first.