# Nginx & Range header & proxy cache

**URL:** <https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770>\
**Category:** NGINX\
**Created:** [September 13, 2011, 12:30pm UTC](https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770 "2011-09-13T12:30:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 13, 2011, 12:30pm UTC](https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770/1 "2011-09-13T12:30:03Z")

</div>

Hi  
If a Range request is sent to nginx proxy (e.g. header “Range:  
bytes=50-”) and the file isn’t in cache yet (cache MISS), the range is  
ignored and full file sent  
When the file has been cached (cache HIT), Range request works as  
expected

Is this the intended behaviour?

Please consider the following example

# /usr/local/nginx/sbin/nginx -V

nginx: nginx version: nginx/1.0.6  
nginx: built by gcc 4.4.5 (Debian 4.4.5-8)  
nginx: configure arguments:

error\_log logs/error-small.log;  
worker\_processes 2;  
events  
{  
use epoll;  
worker\_connections 16384;  
}

http  
{  
include mime.types;  
default\_type application/octet-stream;  
keepalive\_timeout 65;  
proxy\_cache\_path /var/cache/nginx levels=2:2:2  
keys\_zone=small:512m inactive=24h max\_size=128G;  
resolver 8.8.8.8;

```
    server
    {
            listen 80;
            server_name _;
            location /
            {
                    proxy_pass 

```

http://$http\_X\_Backend:$http\_X\_Backend\_Port;  
proxy\_cache small;  
proxy\_cache\_valid 200 302 10y;  
proxy\_cache\_valid 404 10m;  
proxy\_cache\_key $uri;  
proxy\_cache\_min\_uses 10;  
proxy\_cache\_use\_stale updating;  
add\_header X-Cached $upstream\_cache\_status;  
}  
error\_page 500 502 503 504 /50x.html;  
location = /50x.html  
{  
root html;  
}  
}  
}

# wget -SO /dev/null --header=“X-Backend: [download.thinkbroadband.com](http://download.thinkbroadband.com)”

–header=“X-Backend-Port: 80” --header=“Range: bytes=50-”  
localhost/5MB.zip  
–2011-09-13 11:33:45-- [http://localhost/5MB.zip](http://localhost/5MB.zip)  
Resolving localhost… ::1, 127.0.0.1  
Connecting to localhost|127.0.0.1|:80… connected.  
HTTP request sent, awaiting response…  
HTTP/1.1 200 OK  
Server: nginx/1.0.6  
Date: Tue, 13 Sep 2011 10:33:45 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  
X-Cached: MISS  
Length: 5242880 (5.0M) [application/zip]  
Saving to: `/dev/null’

(try 9 more times to get it cached)

# wget -SO /dev/null --header=“X-Backend: [download.thinkbroadband.com](http://download.thinkbroadband.com)”

–header=“X-Backend-Port: 80” --header=“Range: bytes=50-”  
localhost/5MB.zip  
–2011-09-13 11:33:11-- [http://localhost/5MB.zip](http://localhost/5MB.zip)  
Resolving localhost… ::1, 127.0.0.1  
Connecting to localhost|127.0.0.1|:80… connected.  
HTTP request sent, awaiting response…  
HTTP/1.1 206 Partial Content  
Server: nginx/1.0.6  
Date: Tue, 13 Sep 2011 10:33:11 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”  
X-Cached: HIT  
Content-Range: bytes 50-5242879/5242880

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [September 13, 2011, 12:30pm UTC](https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770/2 "2011-09-13T12:30:41Z")

</div>

Hello!

On Tue, Sep 13, 2011 at 10:29:21AM +0000, Richard K. wrote:

> Hi  
> If a Range request is sent to nginx proxy (e.g. header “Range: bytes=50-”) and  
> the file isn’t in cache yet (cache MISS), the range is ignored and full file sent  
> When the file has been cached (cache HIT), Range request works as expected
> 
> Is this the intended behaviour?

Yes.

Maxim D.

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [September 13, 2011, 1:57pm UTC](https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770/3 "2011-09-13T13:57:54Z")

</div>

> Is this the intended behaviour?

> Yes.

Can your ngx\_http\_bytes\_filter\_module be used to get around this  
problem?

---

<div class="post-metadata">

**Author:** ![rkoskela](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rkoskela](https://www.ruby-forum.com/u/rkoskela)\
**Post date:** [September 13, 2011, 6:05pm UTC](https://www.ruby-forum.com/t/nginx-range-header-proxy-cache/211770/4 "2011-09-13T18:05:30Z")

</div>

In relation to this,  
If the file is incomplete on cache (for some reason nginx kept a failed  
cache attempt?) . When range is inside the content-length but more than  
the size of the (incomplete) cached file, it returns 502

I actually posted about it before here but forgot about it

> **[proxy\_cache incomplete file?](https://forum.nginx.org/read.php?2,190276,190276)**
>
> Hi Guys, please observe the following... wget -S...

Is there any possible fix for this?
