# X-Accel-Redirect, proxy\_pass and Range Requests

**URL:** <https://www.ruby-forum.com/t/x-accel-redirect-proxy-pass-and-range-requests/196088>\
**Category:** NGINX\
**Created:** [October 6, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-proxy-pass-and-range-requests/196088 "2010-10-06T17:32:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Timby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@Ben\_Timby](https://www.ruby-forum.com/u/Ben_Timby)\
**Post date:** [October 6, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-proxy-pass-and-range-requests/196088/1 "2010-10-06T17:32:00Z")

</div>

I have some complex interaction going on here and I would like to see  
if anybody has some suggestions for me.

I have Nginx in front of apache serving static files, performing  
compression and SSL. For large files, I use X-Accel-Redirect header to  
offload the sending to nginx. This all works beautifully.

However, the problem is that the backend tracks downloads, it only  
allows a certain number of downloads per file. Some of these files are  
PDFs that are opened on the client side using Acrobat Reader. Acrobat  
Reader sends Range requests to nginx.

Nginx will then forward the range request to apache, which returns a  
200 including the X-Accel-Redirect header. Nginx then pulls the  
correct range from the indicated file and returns that to the client  
with a 206 status.

The problem is, each of these range requests is counted by my backend  
as a download, expiring the download counter prematurely.

My thought on a possible solution is to simply turn on caching in  
nginx, allowing it to satisfy subsequent range requests without  
hitting the backend. Something like the following:

proxy\_cache “zone\_name”;  
proxy\_cache\_key “$host$request\_uri$cookie\_sessionid”;  
proxy\_cache\_valid 206 1m;

However, I am curious if the proxy\_cache\_valid directive matches on  
the response code of the backend, or the response code that nginx will  
send to the client. Also, how does proxy\_cache interact with  
X-Accel-Redirect?

---

<div class="post-metadata">

**Author:** ![Ben\_Timby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@Ben\_Timby](https://www.ruby-forum.com/u/Ben_Timby)\
**Post date:** [October 6, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-proxy-pass-and-range-requests/196088/2 "2010-10-06T18:03:36Z")

</div>

Hello!

On Wed, Oct 06, 2010 at 11:30:44AM -0400, Ben Timby wrote:

> Reader sends Range requests to nginx.
> 
> Nginx will then forward the range request to apache, which returns a  
> 200 including the X-Accel-Redirect header. Nginx then pulls the  
> correct range from the indicated file and returns that to the client  
> with a 206 status.
> 
> The problem is, each of these range requests is counted by my backend  
> as a download, expiring the download counter prematurely.

Even normal request may easily fail somewhere in the middle, and  
even if nginx thinks it sent full response to client. Counting  
downloads is just unreliable.

> send to the client. Also, how does proxy\_cache interact with  
> X-Accel-Redirect?

X-Accel-Redirect responses aren’t cached.

Maxim D.
