skip to content

In a Rails 8.1 app, what does bin/rails middleware print, and why does the order of the default stack matter?

level: middleimportance: must knowfreq 45%

answer

  1. use lines, then run routes
  2. outermost first, router last
  3. Static sits above Executor
  4. Cookies, session store, then Flash
  5. Reloader only when reloading

basics

~20 s

bin/rails middleware prints the built stack for the current environment, outermost first, ending with run App.routes. Order matters because each middleware wraps everything below it: it decides what runs before it, what it can short-circuit and which exceptions it catches.

solid answer

~40 s

`bin/rails middleware` boots the app and prints one `use` line per middleware, top first, then `run ParcelTrack::Application.routes`, the router at the bottom. Each entry wraps everything below it, so order is behaviour. `ActionDispatch::Static` sits above `ActionDispatch::Executor`, so a file in `public/` is served without touching the query cache or the session. `ActionDispatch::RequestId` comes before `Rails::Rack::Logger` so log tags can use the id. `ActionDispatch::Cookies`, the session store and `ActionDispatch::Flash` stack in that order because each needs the one before. `ActionDispatch::ShowExceptions` and `ActionDispatch::DebugExceptions` only catch what is raised below them. The list is per environment: `ActionDispatch::Reloader` appears only when reloading is enabled, so run `RAILS_ENV=production bin/rails middleware` to see production's stack.

code

bash · 2 lines
bash
bin/rails middleware
RAILS_ENV=production bin/rails middleware

go deeper

for a junior

Recall that bin/rails middleware lists the stack outermost first and ends with the router, and that a request passes every listed middleware before reaching a controller.

for a middle

Explain a few orderings with reasons: Static above Executor, RequestId above the logger, Cookies then session then Flash, and why ETag sits below ConditionalGet.

for a senior

Compare stacks across environments when behaviour differs, and know which entries depend on settings: Reloader, ActionableExceptions, ServerTiming, HostAuthorization, SilenceRequest and the SSL pair.

for a principal

Treat the stack as per-request cost and risk: every global middleware runs on every request, so weigh where cross-cutting concerns live and keep the stack lean and documented.

## What the command prints `bin/rails middleware` boots the application in the current environment and prints the **built** middleware stack: one `use` line per middleware, outermost first, followed by `run ParcelTrack::Application.routes`. The last line is the router, the innermost Rack app that every request reaches only after passing every middleware above it. The output uses the same `use`/`run` shape as a Rack `config.ru`, because it describes the same thing: an ordered chain that a request walks down and a response walks back up. The stack differs by environment, because several entries are added only when a setting is on. `RAILS_ENV=production bin/rails middleware` shows what production actually runs. ## The default Rails 8.1 stack, top to bottom (main entries) | Middleware | Job | Present when | |---|---|---| | `ActionDispatch::HostAuthorization` | rejects requests for unlisted hosts | `config.hosts` is not empty (development by default) | | `Rack::Sendfile` | lets a proxy send file bodies | always | | `ActionDispatch::Static` | serves files from `public/` | `config.public_file_server.enabled` (true by default) | | `ActionDispatch::Executor` | wraps the request as one unit of work | always | | `ActionDispatch::ServerTiming` | `Server-Timing` header | `config.server_timing` (on in the development template) | | `Rack::Runtime` | `x-runtime` header | always | | `Rack::MethodOverride` | turns a POST with `_method` into PATCH, PUT or DELETE | not API-only | | `ActionDispatch::RequestId` | sets `X-Request-Id`, `request.request_id` | always | | `ActionDispatch::RemoteIp` | computes `request.remote_ip` | always | | `Rails::Rack::Logger` | the `Started GET` line and log tags | always | | `ActionDispatch::ShowExceptions` | renders exceptions through the exceptions app | always | | `ActionDispatch::DebugExceptions` | logs exceptions, debug page for local requests | always | | `ActionDispatch::Reloader` | reloads changed code | reloading enabled | | `ActionDispatch::Callbacks` | before/after dispatch callbacks | always | | `ActionDispatch::Cookies`, session store, `ActionDispatch::Flash` | cookie jar, session, flash | not API-only | | `Rack::Head`, `Rack::ConditionalGet`, `Rack::ETag` | HEAD bodies, 304s, ETags | always | Frameworks and gems insert their own entries relative to these: Propshaft puts `Propshaft::Server` before the executor in development and test, the cache store's local-cache middleware goes before `Rack::Runtime`, and the development template's `migration_error = :page_load` adds `ActiveRecord::Migration::CheckPending` after `ActionDispatch::Callbacks`. ## Why the order is behaviour Each middleware receives the rest of the stack as a single app. That gives position four consequences: - **Short-circuiting saves work.** `ActionDispatch::Static` answers before the executor runs, so a stylesheet in `public/` costs no executor callbacks, no session decode and no routing. `ActionDispatch::HostAuthorization` sits at the top so a blocked host is refused before anything else runs. - **Later layers depend on earlier ones.** `ActionDispatch::RequestId` must run before `Rails::Rack::Logger` so `config.log_tags = [:request_id]` has a value. The session store reads cookies, so it sits below `ActionDispatch::Cookies`; flash lives in the session, so `ActionDispatch::Flash` sits below the session store. - **Exception coverage is one-directional.** `ActionDispatch::ShowExceptions` and `ActionDispatch::DebugExceptions` see exceptions raised by the reloader, callbacks, session, router and controller below them. An exception raised in a middleware placed above them never reaches them, so neither the debug page nor the exceptions app renders it. - **Responses are post-processed bottom-up.** `Rack::ETag` sits below `Rack::ConditionalGet`, so the ETag is computed first and the conditional-GET layer, wrapping it, can compare it with `If-None-Match` and turn the response into a 304. ## Environment differences worth knowing 1. A generated production app has no `ActionDispatch::Reloader` (reloading is off), no `ActionDispatch::ActionableExceptions` (`config.consider_all_requests_local` is false), no `ActionDispatch::ServerTiming`, no `CheckPending` and no `Propshaft::Server`. 2. Production gains `Rails::Rack::SilenceRequest` because the generated `production.rb` sets `config.silence_healthcheck_path = "/up"`, and `ActionDispatch::AssumeSSL` or `ActionDispatch::SSL` when `config.assume_ssl` or `config.force_ssl` is on. 3. Development-only gems add more, such as `WebConsole::Middleware` before `ActionDispatch::DebugExceptions`. ## Reading the output when debugging The list answers practical questions quickly: - **A header is missing on some responses.** Find the middleware that adds it and check whether a layer above it, such as `ActionDispatch::Static`, answered first. - **A gem changed behaviour after an upgrade.** Diff the output before and after; gems insert their middleware relative to framework entries, so a moved anchor moves their layer too. - **`request.remote_ip` is wrong behind a proxy.** `ActionDispatch::RemoteIp` computes it once, high in the stack, from the trusted proxy settings; everything below reads that value. - **The log line has no request id.** Check that `ActionDispatch::RequestId` still sits above `Rails::Rack::Logger` after any custom insertions. The practical habit: when a header, a log tag or a session value behaves differently between environments, print both stacks and compare them before reading any code.

  • In a Rails 8.1 app, why does a file in public/ not open a database connection or decode the session?
    `ActionDispatch::Static` sits above `ActionDispatch::Executor`, the session store and the router. When the path matches a file, it returns the response itself and never calls the rest of the stack, so no executor callbacks, query cache, cookie or session work run for that request.
  • In a Rails 8.1 app, why can a log line carry the request id when config.log_tags is [:request_id]?
    `ActionDispatch::RequestId` runs above `Rails::Rack::Logger`. By the time the logger middleware builds its tags, the request id is already set from the incoming `X-Request-Id` header or generated, so every line logged for that request can be tagged with it.

  • The first use line printed by bin/rails middleware is the innermost middleware.
  • bin/rails routes prints the middleware stack after the route table.
  • ActionDispatch::Reloader runs in production to pick up deployed code.
  • ShowExceptions catches exceptions raised by any middleware, above or below it.
  • Middleware order is cosmetic because every middleware runs anyway.

Assumes Rails 8.1 with Propshaft, the only asset pipeline a Rails 8 app is generated with; the exact list varies by environment, settings and gems.