FAQ

Page Discussion Edit History

NginxXSendfile

The delivery of a static file which depends on an application header is known as the X-Sendfile feature.

Lighttpd has this feature and there is a mod_xsendfile for Apache2 .

Nginx also has this feature, but implemented a little bit differently. In Nginx this feature is called X-Accel-Redirect.

There are two main differences:

  1. The header must contain a URI
  2. The location should be defined as internal; to prevent the client going directly to the URI

Example nginx configuration:

location /protected/ {
  internal;
  root   /some/path;
}

If the application does this (pseudo-code):

  add_header("X-Accel-Redirect: /protected/iso.img");

...then nginx will send /some/path/protected/iso.img (note that root and internal redirect path are concatenated) to the client ;-) .

If you want to deliver /some/path/iso.img then configure nginx like this:

location /protected/ {
  internal;
  alias   /some/path/; # note the trailing slash
}

You should also know that the following headers aren't modified by nginx:

Content-Type
Content-Disposition
Accept-Ranges
Set-Cookie
Cache-Control
Expires

If some of these header lines are not set, then they will be set by the redirected response.

The application can also have a limited control over the process, sending the following headers prior to X-Accel-Redirect.

X-Accel-Limit-Rate: 1024
X-Accel-Buffering: yes|no
X-Accel-Charset: utf-8


Edit section: Links to this issue Links to this issue

Using X-Accel-Redirect Header With Nginx to Implement Controlled Downloads (with rails and php examples) from Alexey Kovyrin

Rails plugin for X-Accel-Redirect

proxy_ignore_headers Ignoring Content Type headers when using X-Accel-Redirect

proxy_hide_header Hide headers from upstream servers when using X-Accel-Redirect