Parent

Class Index [+]

Quicksearch

Rack::Sendfile

Sendfile

The Sendfile middleware intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. The web server is then responsible for writing the file contents to the client. This can dramatically reduce the amount of work required by the Ruby backend and takes advantage of the web server’s optimized file delivery code.

In order to take advantage of this middleware, the response body must respond to to_path and the request must include an X-Sendfile-Type header. Rack::File and other components implement to_path so there’s rarely anything you need to do in your application. The X-Sendfile-Type header is typically set in your web servers configuration. The following sections attempt to document

Nginx

Nginx supports the X-Accel-Redirect header. This is similar to X-Sendfile but requires parts of the filesystem to be mapped into a private URL hierarachy.

The following example shows the Nginx configuration required to create a private “/files/” area, enable X-Accel-Redirect, and pass the special X-Sendfile-Type and X-Accel-Mapping headers to the backend:

  location ~ /files/(.*) {
    internal;
    alias /var/www/$1;
  }

  location / {
    proxy_redirect     off;

    proxy_set_header   Host                $host;
    proxy_set_header   X-Real-IP           $remote_addr;
    proxy_set_header   X-Forwarded-For     $proxy_add_x_forwarded_for;

    proxy_set_header   X-Sendfile-Type     X-Accel-Redirect;
    proxy_set_header   X-Accel-Mapping     /files/=/var/www/;

    proxy_pass         http://127.0.0.1:8080/;
  }

Note that the X-Sendfile-Type header must be set exactly as shown above. The X-Accel-Mapping header should specify the name of the private URL pattern, followed by an equals sign (=), followed by the location on the file system that it maps to. The middleware performs a simple substitution on the resulting path.

See Also: wiki.codemongers.com/NginxXSendfile

lighttpd

Lighttpd has supported some variation of the X-Sendfile header for some time, although only recent version support X-Sendfile in a reverse proxy configuration.

  $HTTP["host"] == "example.com" {
     proxy-core.protocol = "http"
     proxy-core.balancer = "round-robin"
     proxy-core.backends = (
       "127.0.0.1:8000",
       "127.0.0.1:8001",
       ...
     )

     proxy-core.allow-x-sendfile = "enable"
     proxy-core.rewrite-request = (
       "X-Sendfile-Type" => (".*" => "X-Sendfile")
     )
   }

See Also: redmine.lighttpd.net/wiki/lighttpd/Docs:ModProxyCore

Apache

X-Sendfile is supported under Apache 2.x using a separate module:

tn123.ath.cx/mod_xsendfile/

Once the module is compiled and installed, you can enable it using XSendFile config directive:

  RequestHeader Set X-Sendfile-Type X-Sendfile
  ProxyPassReverse / http://localhost:8001/
  XSendFile on

Constants

F

Public Class Methods

new(app, variation=nil) click to toggle source
     # File lib/rack/sendfile.rb, line 101
101:     def initialize(app, variation=nil)
102:       @app = app
103:       @variation = variation
104:     end

Public Instance Methods

call(env) click to toggle source
     # File lib/rack/sendfile.rb, line 106
106:     def call(env)
107:       status, headers, body = @app.call(env)
108:       if body.respond_to?(:to_path)
109:         case type = variation(env)
110:         when 'X-Accel-Redirect'
111:           path = F.expand_path(body.to_path)
112:           if url = map_accel_path(env, path)
113:             headers[type] = url
114:             body = []
115:           else
116:             env['rack.errors'] << "X-Accel-Mapping header missing"
117:           end
118:         when 'X-Sendfile', 'X-Lighttpd-Send-File'
119:           path = F.expand_path(body.to_path)
120:           headers[type] = path
121:           body = []
122:         when '', nil
123:         else
124:           env['rack.errors'] << "Unknown x-sendfile variation: '#{variation}'.\n"
125:         end
126:       end
127:       [status, headers, body]
128:     end

Private Instance Methods

map_accel_path(env, file) click to toggle source
     # File lib/rack/sendfile.rb, line 137
137:       def map_accel_path(env, file)
138:         if mapping = env['HTTP_X_ACCEL_MAPPING']
139:           internal, external = mapping.split('=', 2).map{ |p| p.strip }
140:           file.sub(/^#{internal}/, external)
141:         end
142:       end
variation(env) click to toggle source
     # File lib/rack/sendfile.rb, line 131
131:       def variation(env)
132:         @variation ||
133:           env['sendfile.type'] ||
134:           env['HTTP_X_SENDFILE_TYPE']
135:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.