rails, rack, unicorn or puma, nginx の関係についてちゃんと理解したい
だいぶメモ書きです。
表題の件、まだ曖昧にしか理解できてない。
ググっても浅い情報しか出てこないし、各ライブラリのソースコードを読んでみたけどどう繋がってるか、それぞれどんな役割を担っているか把握するのムズいなぁ…。
結論
色々調べてここまで分かった。
- 「クライアント側
nginx
<->puma
orunicorn
<->rack
<->rails
DB 側」 的な感じ - rails(や sinatra)はつまるところ rack の仕様に沿って
env
(HTTPリクエスト)をもらって[status, headers, body]
(HTTP レスポンス)を返すだけのプログラムである
rack
HTTP server としてのインターフェースを提供する。 #call
とか。
rails
rack アプリケーション。 rack の仕様に沿った #call
を実装する。
とりあえず actionpack の Action Dispatch
でリクエストをさばき、 Action Controller
でレスポンスを生成していく感じなのかな?
Response#rack_response でレスポンス生成してる?
ちなみに以下のようなコードを差し込んだ時のログはこんな感じ。
class HogeController < ApplicationController def index caller.each{ |line| p line} ... end end
Started GET "/" for 127.0.0.1 at 2019-09-17 11:10:00 +0900 Processing by WelcomeController#index as HTML "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/abstract_controller/base.rb:194:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/rendering.rb:30:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/abstract_controller/callbacks.rb:42:in `block in process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:132:in `run_callbacks'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/abstract_controller/callbacks.rb:41:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/rescue.rb:22:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/notifications.rb:168:in `block in instrument'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/notifications/instrumenter.rb:23:in `instrument'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/notifications.rb:168:in `instrument'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/instrumentation.rb:32:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.0/lib/active_record/railties/controller_runtime.rb:24:in `process_action'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/abstract_controller/base.rb:134:in `process'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionview-5.2.0/lib/action_view/rendering.rb:32:in `process'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal.rb:191:in `dispatch'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_controller/metal.rb:252:in `dispatch'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:34:in `serve'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:52:in `block in serve'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:35:in `each'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/journey/router.rb:35:in `serve'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/routing/route_set.rb:840:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:190:in `call!'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:168:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/omniauth-1.8.1/lib/omniauth/builder.rb:63:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/tempfile_reaper.rb:15:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/etag.rb:25:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/conditional_get.rb:25:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/http/content_security_policy.rb:18:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:232:in `context'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/cookies.rb:670:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.0/lib/active_record/migration.rb:559:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/callbacks.rb:98:in `run_callbacks'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/callbacks.rb:26:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/executor.rb:14:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/web-console-3.6.2/lib/web_console/middleware.rb:135:in `call_app'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/web-console-3.6.2/lib/web_console/middleware.rb:30:in `block in call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/web-console-3.6.2/lib/web_console/middleware.rb:20:in `catch'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/web-console-3.6.2/lib/web_console/middleware.rb:20:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:38:in `call_app'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:26:in `block in call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:71:in `block in tagged'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:28:in `tagged'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/tagged_logging.rb:71:in `tagged'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/rack/logger.rb:26:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/quiet_assets.rb:13:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/request_id.rb:27:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.0/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/executor.rb:14:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.0/lib/action_dispatch/middleware/static.rb:127:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/engine.rb:524:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/puma-3.11.4/lib/puma/configuration.rb:225:in `call'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/puma-3.11.4/lib/puma/server.rb:632:in `handle_request'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/puma-3.11.4/lib/puma/server.rb:446:in `process_client'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/puma-3.11.4/lib/puma/server.rb:306:in `block in run'" "/Users/daidoshota/training/perfect_rails_awesome_events/vendor/bundle/ruby/2.5.0/gems/puma-3.11.4/lib/puma/thread_pool.rb:120:in `block in spawn_thread'" Rendering welcome/index.html.erb within layouts/application Event Load (3.2ms) SELECT "events".* FROM "events" WHERE (start_time > '2019-09-17 02:10:00.146381') ORDER BY "events"."start_time" ASC ↳ app/views/welcome/index.html.erb:6 Rendered welcome/index.html.erb within layouts/application (17.7ms) Completed 200 OK in 96ms (Views: 70.1ms | ActiveRecord: 13.1ms)
ラストが actionpack/lib/action_controller/metal/basic_implicit_render.rb の send_action
。
最後まで追えてない気がするけど。
module function Kernel.#caller (Ruby 2.6.0)
unicorn
rack アプリケーション用サーバ。 マルチプロセス。 HTTPリクエストを受け取って rack インタフェースにそったオブジェクトを rack アプリケーションに渡してくれるWebサーバ、だけど rack gem に依存はしていない。(開発環境のみ依存)
RackがよくわからなかったのでRackアプリケーションをUnicornで動かしてnginxからリクエストを転送してみた - woshidan's blog
puma
rack ハンドラ(rack アプリケーション用サーバと同義?)マルチプロセス&マルチスレッド。
unicorn を代替するために作られた? Heroku が推してる。