FAQ

Page Discussion Edit History

ChsXSendfile

(Redirected from NginxChsXSendfile)

基于应用程序的header来发送静态文件的特性叫做 X-Sendfile.

Lighttpd 有该特性,apache也有个模块 叫 mod_xsendfile .

Nginx也有该功能,但实现得稍微有些不同.Ngnix该功能叫做X-Accel-Redirect.

主要有两个不同之处:

  1. header 必须包含 URI
  2. location 必须 被定义为 internal; 以阻止客户端直接访问该 URI

Nginx 配置样例:

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

如果应用程序这么做了 (伪代码):

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

...然后nginx将发送文件 /some/path/protected/iso.img (注意:root路径和内部跳转路径拼接在一起了) 给客户端.

如果你想发送文件 /some/path/iso.img ,那么配置文件应该这么写:

location /protected/ {
  internal;
  alias   /some/path/; # 留意末尾的/
}

Nginx将不改变以下的headers信息:

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

如果未设置这些header之一,它们将由重定向的响应设置.

应用程序在处理中也可以设置有限的控制,在 X-Accel-Redirect 之前 发送如下的header.

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


[edit] Links to this issue

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