【问题标题】:Routing while upgrading ruby/rails and moving from mongrel to unicorn升级 ruby​​/rails 并从 mongrel 迁移到 unicorn 时进行路由
【发布时间】:2014-03-29 22:27:05
【问题描述】:

我有一个项目,我正在升级 ruby​​-1.8.7-p72/rails 2.3.2/mongrel 应用程序。我采取的第一步是升级到 ruby​​-1.9.3-p484 和 rails 2.3.18 和 unicorn。

我现在可以通过 unicorn 让应用程序在开发服务器上运行(通过 capistrano - rvm-capistrano 和 capistrano-unicorn 部署)。但是,它无法提供任何资产(图片、样式表等)

我在独角兽日志中看到如下错误:

ActionController::RoutingError (No route matches "/images/pp.jpg" with {:method=>:get}):
<internal:prelude>:10:in `synchronize'
unicorn (4.8.2) lib/unicorn/http_server.rb:572:in `process_client'
unicorn (4.8.2) lib/unicorn/http_server.rb:666:in `worker_loop'
unicorn (4.8.2) lib/unicorn/http_server.rb:521:in `spawn_missing_workers'
unicorn (4.8.2) lib/unicorn/http_server.rb:140:in `start'
unicorn (4.8.2) bin/unicorn_rails:209:in `<top (required)>'
/Users/ruby/.rvm/gems/ruby-1.9.3-p484/bin/unicorn_rails:19:in `load'
/Users/ruby/.rvm/gems/ruby-1.9.3-p484/bin/unicorn_rails:19:in `<main>'
/Users/ruby/.rvm/gems/ruby-1.9.3-p484/bin/ruby_executable_hooks:15:in `eval'
/Users/ruby/.rvm/gems/ruby-1.9.3-p484/bin/ruby_executable_hooks:15:in `<main>'

无论是直接与独角兽端口通信还是通过 apache(mod_rewrite - 与独角兽端口通信),我都会看到同样的错误

我需要更改资产的位置吗?目前他们在 /public 下。它们在 ruby​​-1.8.7-p72/rails 2.3.2/mongrel 下工作正常,但在 ruby​​-1.9.3-p484/rails 2.3.18/unicorn 下不能正常工作。或者是否有一个配置项可以放入我的 cap 文件中来设置资产的位置?我在 unicorn 文档中没有找到任何内容。

还有其他人经历过这个并知道答案吗?我会继续努力,但任何提示将不胜感激。

【问题讨论】:

  • 我认为这是因为我的实例没有在开发模式下启动,因此它试图通过 apache 提供资产。作为解决这个问题的一部分,我现在意识到我还必须扩展我的 unicorn 配置文件,所以现在就开始处理这个......
  • 现在我正在开发模式下启动并读取正确的配置文件,但我仍然遇到问题。因为我还在 rails2 我不能使用 config.serve_static_assets = true。 Webbrick 可以很好地服务资产,但我仍然没有找到让独角兽为它们服务的方法。

标签: ruby-on-rails unicorn mongrel


【解决方案1】:

我终于得到了这个工作....多么痛苦。我很高兴我只需要这样做一次。实际上,我只在我的开发服务器上运行它。由于缺少 serve_static_assets 配置值,我认为在我移至 rails 3 之前,我无法在我的开发计算机上使用 unicorn。

我的独角兽配置文件​​如下所示:

# config/unicorn/development-mini-2.rb
# 
# this is launched from capistrano. see config/deploy/dev2.rb.
#
puts "reading development-mini-2.rb"

rails_env = ENV['RAILS_ENV'] || 'development'

timeout 30
worker_processes 4
listen 8031

root = '/Users/ruby/apps/app'
working_directory "#{root}/current"
pid "#{root}/shared/pids/unicorn.app.pid"
stderr_path "#{root}/shared/log/unicorn.log"
stdout_path "#{root}/shared/log/unicorn.log"

preload_app true

GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true


before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

我的帽子部署看起来像:

#
# deployment for the dev mini server instance 2
# app.example.com
# deploy with
# 
#
require 'rvm/capistrano'

set :application, "app"

set :rvm_ruby_string, 'ruby-1.9.3-p484'
set :rails_env, 'development'

server 'app.example.com', :app, :web, :db, :primary => true

set :user,  "ruby"
set :group, "ruby"

set :deploy_to,   "/Users/ruby/apps/#{application}"
set :copy_remote_dir, '/Users/ruby/tmp'


namespace :deploy do
  desc "Start unicorn"
  task :start, :except => { :no_release => true } do
    run "cd #{deploy_to}/current ; unicorn_rails -c config/unicorn/development-mini-2.rb -D"
    run "ps aux | grep unicorn_rails | head -n 1 | awk '{print $2}' > #{deploy_to}/shared/pids/unicorn.app.pid"
  end

  desc "Stop unicorn"
  task :stop, :except => { :no_release => true } do
    run "kill -s QUIT `cat #{deploy_to}/shared/pids/unicorn.app.pid`"
  end

  desc "Restart Unicorn"
  task :restart, :except => { :no_release => true } do
    run "kill -s USR2 `cat #{deploy_to}/shared/pids/unicorn.app.pid`"
  end  
end

我的 apache conf 看起来像:

<VirtualHost *:443>
    ServerName app.example.com
    DocumentRoot /Users/ruby/apps/app/current/public

    RewriteEngine On
    #RewriteLog "/var/log/apache2/rewrite.log"
    #RewriteLogLevel 3

        <Proxy "balancer://unicornservers2">
        Order deny,allow
        Allow from any
                BalancerMember http://localhost:8031
        </Proxy>

    # Redirect all non-static requests to unicorn
    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ balancer://unicornservers2%{REQUEST_URI} [P,QSA,L]

    RequestHeader set X-Forwarded-Proto "https"

    CustomLog /var/log/apache2/access_log "%h %l %u %t \"%r\" %>s %b"

    <IfModule mod_ssl.c>
        SSLEngine On
        SSLCipherSuite "ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM"
        SSLProtocol -ALL +SSLv3 +TLSv1
        SSLCertificateFile "/etc/certificates/example.com.0CB3DB818A24F638D4D4A5664865B68ACB924FF1.cert.pem"
        SSLCertificateKeyFile "/etc/certificates/example.com.0CB3DB818A24F638D4D4A5664865B68ACB924FF1.key.pem"
        SSLCertificateChainFile "/etc/certificates/example.com.0CB3DB818A24F638D4D4A5664865B68ACB924FF1.chain.pem"
        SSLProxyProtocol -ALL +SSLv3 +TLSv1
    </IfModule>

</VirtualHost>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多