In Rails 8.1, what does setting config.action_dispatch.x_sendfile_header change about how send_file delivers a file?
answer
- Rack::Sendfile is already in the stack
- default value is nil
- body must respond to to_path
- empty body, content-length 0
- X-Accel-Redirect for nginx
basics
~20 sx_sendfile_header tells Rack::Sendfile which header the front server understands. send_file's body responds to to_path, so the middleware drops the body and sends only that header; the front server then reads and sends the file instead of Ruby.
solid answer
~40 sBy default the key is `nil`, so `send_file`'s body, an object that responds to `to_path`, is read by Ruby in 16 KB chunks and pushed through the web server thread for the whole download. `Rack::Sendfile` is always in the middleware stack; once `config.action_dispatch.x_sendfile_header` is set to `"X-Sendfile"` (Apache) or `"X-Accel-Redirect"` (nginx), it replaces that body with an empty one, sets `content-length: 0` and adds the header with the file's path or mapped internal URI, and the front server delivers the bytes. Only `to_path` bodies qualify, so `send_data` is never offloaded, and the front server must see the same file. Current Rack releases ignore a request's `X-Sendfile-Type` header, so the config value is the switch.
code
ruby · 8 lines# config/environments/production.rb
Rails.application.configure do
# nginx in front: answer with an internal URI instead of the bytes
config.action_dispatch.x_sendfile_header = "X-Accel-Redirect"
# Apache with mod_xsendfile would use:
# config.action_dispatch.x_sendfile_header = "X-Sendfile"
endgo deeper
Recall that send_file can let the front web server send the file, and that config.action_dispatch.x_sendfile_header is the setting that turns this on.
Explain the mechanism: send_file's body responds to to_path, Rack::Sendfile swaps it for an empty body plus X-Sendfile or X-Accel-Redirect, and only to_path bodies qualify.
Cover the operational traps: the default is nil, the front server must see the same path, nginx needs x-accel-mapping, and current Rack ignores a request's X-Sendfile-Type for security.
Weigh where file delivery belongs: Ruby threads, a front server, or object storage with signed URLs, against authorization needs and the deployment's filesystem layout.
## The default: Ruby copies the bytes `send_file` does not read the file itself. It sets the headers and gives the response a small body object that knows the path (it responds to **`to_path`**) and, when iterated, opens the file and yields it in **16 KB chunks** (`file.read(16384)`). With nothing else configured, the Ruby web server thread that handled the request iterates that body and copies every chunk to the socket. A 200 MB payroll archive downloaded over a slow connection therefore keeps a Ruby thread busy for the whole download. ## What x_sendfile_header turns on Rails' default middleware stack always includes **`Rack::Sendfile`**, built with the value of `config.action_dispatch.x_sendfile_header`. Action Dispatch's railtie sets that key to **`nil`**, and the Rails 8.1 `production.rb` template has no line for it, so a new app does nothing extra. Set it to the header your front server understands: | value | front server | what Rack::Sendfile writes | |---|---|---| | `"X-Sendfile"` | Apache with mod_xsendfile, or any server that reads that header | the absolute file path | | `"X-Lighttpd-Send-File"` | lighttpd | the absolute file path | | `"X-Accel-Redirect"` | nginx | an internal URI, mapped from the file path | When the response body responds to `to_path` and a variation is set, `Rack::Sendfile` **replaces the body with an empty one**, sets `content-length: 0` and adds the header. The front server sees it, opens the file and streams it with its own optimised file delivery, while the Ruby thread is free as soon as the headers leave. ## Rules that follow from the mechanism 1. **Only `to_path` bodies qualify.** `send_file` qualifies; `send_data`, `render` output and `send_stream` from `ActionController::Live` do not, because their bytes exist only in Ruby. 2. **Both processes must see the same file.** `X-Sendfile` carries an absolute path, so the front server must be able to read that path on its own filesystem; in containers that means a shared volume. 3. **nginx needs a mapping.** For `X-Accel-Redirect` the absolute path is rewritten to an `internal` location. Because Rails passes no mappings of its own, `Rack::Sendfile` reads the `x-accel-mapping` header that the nginx proxy sets (for example `/var/app/storage/=/protected/`); without it Rack logs `x-accel-mapping header missing` and Ruby sends the bytes after all. 4. **The controller code does not change.** Authorization still happens in the action; only the delivery moves. ## The request-header switch is gone Older setups let the proxy turn sendfile on per request by sending an `X-Sendfile-Type` request header, and the comment on `send_file` in Rails 8.1.4 still mentions it. Rack's October 2025 security releases (2.2.20, 3.1.18 and 3.2.3, for CVE-2025-61780) stopped reading that header: a client could forge it to switch on X-Accel-Redirect and bypass the proxy's restrictions. Since then the variation comes only from the middleware's constructor, which in Rails means `config.action_dispatch.x_sendfile_header`, or from a `sendfile.type` value a server puts in the Rack env. Likewise `x-accel-mapping` is only honoured when `X-Accel-Redirect` was configured explicitly. ## How to confirm it works 1. Request a download with `curl -I` through the front server and through Puma directly. Behind a working setup, the response Rails produces carries the sendfile header and `content-length: 0`, while the response the client receives has the real length. 2. Watch the Rails log: the request finishes in a few milliseconds even for a large file, because the action only set headers. 3. If the client receives an empty file, the front server is not acting on the header (module not loaded, or the path is not visible to it); if Ruby still streams, the variation or the nginx mapping is missing. ## What the guide gets wrong The Action Controller Advanced Topics guide says `send_file` streams in 4 kB blocks and offers `:stream` and `:buffer_size` options. Neither option exists in the Rails 8.1.4 source, and the chunk size is 16 KB. Trust the source. ## Where it fits today Rails 8.1 apps generated with Kamal put Thruster in front of Puma, and Thruster has its own X-Sendfile handling; how that is wired is a deployment question. The Rails side is the same everywhere: `send_file` produces a `to_path` body, and `x_sendfile_header` decides whether Ruby or the front server pushes the bytes.
- Why does a large send_data export not benefit from x_sendfile_header?`Rack::Sendfile` only acts on bodies that respond to `to_path`, meaning a file the front server can open. `send_data` renders a String held in Ruby memory, so there is no path to hand over and Ruby sends the bytes itself. Write the export to a file and use `send_file`, or stream it with `send_stream`.
- With X-Accel-Redirect configured, the logs say "x-accel-mapping header missing" and downloads are slow. What is wrong?`Rack::Sendfile` needs a mapping from the file's absolute path to an nginx `internal` location. Rails passes no mappings, so it reads the `x-accel-mapping` header from the proxy; nginx is not sending it. Without a mapping the middleware leaves the body alone and Ruby streams the file. Add `proxy_set_header x-accel-mapping` with the storage-to-internal pair.
A shop assistant who, instead of fetching a heavy parcel from the back room, writes its shelf number on a slip and hands the slip to the loading dock: the dock moves the parcel, and the assistant is free for the next customer as soon as the slip leaves their hand.
- The Rails 8.1 production.rb template already sets x_sendfile_header for you.
- x_sendfile_header also offloads large send_data responses to the front server.
- send_file reads in 4 KB blocks, tunable through a :buffer_size option.
- A proxy can switch sendfile on by sending X-Sendfile-Type on each request.
- With X-Sendfile set, Ruby still sends the bytes and the header is only a hint.
Assumes Rails 8.1 on a Rack release with the October 2025 Rack::Sendfile fix (2.2.20, 3.1.18, 3.2.3 or later), which ignores the request's X-Sendfile-Type header.