原文:

http://nginx.2469901.n2.nabble.com/nginx-amp-Range-header-amp-proxy-cache-td6786959.html

nginx配置见原文

关于HTTP请求头range的说明:

http://www.iwms.net/n2030c40.aspx

请求测试:

wget -SO /dev/null --header="X-Backend: download.thinkbroadband.com" --header="X-Backend-Port: 80" --header="Range: bytes=50-" localhost/5MB.zip
--2013-07-02 22:43:30--  http://localhost/5MB.zip
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response...
 HTTP/1.1 200 OK
 Server: nginx/0.8.54
 Date: Tue, 02 Jul 2013 14:43:31 GMT
 Content-Type: application/zip
 Connection: keep-alive
 Last-Modified: Mon, 02 Jun 2008 15:30:42 GMT
 ETag: "11f000b-500000-44eb0adaf4c80"
 Accept-Ranges: bytes
 Content-Length: 5242880
 Access-Control-Allow-Origin: *
 X-Cached: MISS
Length: 5242880 (5.0M) [application/zip]
Saving to: `/dev/null'

100%[==========================================================================================>] 5,242,880    638K/s   in 11s    

2013-07-02 22:43:43 (453 KB/s) - `/dev/null' saved [5242880/5242880]

第一次请求时nginx代理上还没有文件的缓存,所以cache miss,代理服务器会全部保存到缓存。

root@wangyangtest:/var/cache/nginx# ls
07
root@wangyangtest:/var/cache/nginx# ll 07/a6/a3/ac136536ba7bb4171654401eb2a3a607

-rw-------  1 nobody  wheel  5243220 Jul  2 22:43 07/a6/a3/ac136536ba7bb4171654401eb2a3a607

发起续传请求:

root@wangyangtest:/var/cache/nginx# wget -SO /dev/null --header="X-Backend: download.thinkbroadband.com" --header="X-Backend-Port: 80" --header="Range: bytes=50-" localhost/5MB.zip
--2013-07-02 22:43:48--  http://localhost/5MB.zip
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response...
 HTTP/1.1 206 Partial Content
 Server: nginx/0.8.54
 Date: Tue, 02 Jul 2013 14:43:48 GMT
 Content-Type: application/zip
 Content-Length: 5242830
 Connection: keep-alive
 Last-Modified: Mon, 02 Jun 2008 15:30:42 GMT
 ETag: "11f000b-500000-44eb0adaf4c80"
 Access-Control-Allow-Origin: *
 X-Cached: HIT
 Content-Range: bytes 50-5242879/5242880

客户端再次发起续传请求,首先要检查Cached,如果命中则正常返回206续传