# X-Accel-Redirect testing

**URL:** <https://www.ruby-forum.com/t/x-accel-redirect-testing/223207>\
**Category:** NGINX\
**Created:** [September 26, 2012, 12:28am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207 "2012-09-26T00:28:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 26, 2012, 12:28am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/1 "2012-09-26T00:28:33Z")

</div>

## Hello, I’d like to test if I set up my configs properly and if they’re working. Should it work if I send a GET request with the X-Accel-Redirect header? I am using Mozilla plugin “Modify Headers”. I have added: “X-Accel-Redirect /index” and my request on the server is: (captured with tcpdump)

## User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive X-Accel-Redirect: /index

## My vhost config in nginx is:

## server { listen 80; server\_name [aa.com](http://aa.com); #it’s added in my hosts file location / { alias /var/www; internal; } }

## Path is:

## ls -lh /var/www/ index

Error is 404 Not Found - nothing logged in error.log though …

My environment would actually be Apache as frontend which passes  
requests  
with X-Accel-Redirect to Nginx which serves static files.  
But right now I’m in testing phase.

Posted at Nginx Forum:

> **[X-Accel-Redirect testing](https://forum.nginx.org/read.php?2,231059,231059#msg-231059)**
>
> Hello, I'd like to test if I set up my configs properly and if they're working. Should it work if I send a GET request with the X-Accel-Redirect header? I am using Mozilla plugin "Modify Headers". I have added: "X-Accel-Redirect...

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [September 26, 2012, 1:07am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/2 "2012-09-26T01:07:07Z")

</div>

On 25 September 2012 23:28, w00t [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I’d like to test if I set up my configs properly and if they’re working.  
> Should it work if I send a GET request with the X-Accel-Redirect header?

No. This is a /response/ header from (typically) a dynamic backend  
which instructs nginx to serve some piece of static content, thus  
freeing up the backend for more intelligent, heavy-weight request  
handling. There are more sophisticated uses, but you should at least  
understand this basic use case before attempting them.

## Regards, Jonathan

Jonathan M. // Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [September 26, 2012, 10:18am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/3 "2012-09-26T10:18:02Z")

</div>

## Jonathan M. Wrote:

> No. This is a /response/ header from (typically) a dynamic backend  
> which instructs nginx to serve some piece of static content, thus  
> freeing up the backend for more intelligent, heavy-weight request  
> handling. There are more sophisticated uses, but you should at least  
> understand this basic use case before attempting them.

Hello Jonathan,

Thanks for the reply. I’d love to at least understand the basics, in  
fact  
this is what I’m trying to do. It’s a bit hard when there’s no thorough  
documentation and all responses found on internet are vague.

You are saying that it will work only when Nginx is a reverse proxy and  
receives a response from another backend, like Apache. But I want Apache  
as  
frontend and Nginx as backend. Should I focus on Xsendfile?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,231059,231071#msg-231071](http://forum.nginx.org/read.php?2,231059,231071#msg-231071)

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [September 26, 2012, 11:04am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/4 "2012-09-26T11:04:12Z")

</div>

On 26 Set 2012 10h17 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the reply. I’d love to at least understand the basics, in  
> fact this is what I’m trying to do. It’s a bit hard when there’s no  
> thorough documentation and all responses found on internet are  
> vague.
> 
> You are saying that it will work only when Nginx is a reverse proxy  
> and receives a response from another backend, like Apache. But I  
> want Apache as frontend and Nginx as backend. Should I focus on  
> Xsendfile?

XSendfile in Nginx is called X-Accel-Redirect.

The way X-Accel-Redirect works is that your app/backend sends a  
“special” header with the location of the file that is to be served by  
Nginx. Usually the application case is for speeding up the delivery of  
protected/private files. Let’s say you have a ecommerce site that  
sells digital downloads. Then these files will be protected and for  
speeding up the delivery your application sends an X-Accel-Redirect  
header with the location of the file. Then the file is delivered by  
Nginx as a regular static file.

Example: I request to download:

> **[404 | Oops deze pagina bestaat niet! Bekijk myshop.com | myShop.com](http://myshop.com/downloads/big_hit.flac)**
>
> 404 | Oops deze pagina bestaat niet! Bekijk myshop.com | myShop.com

The file is stocked at /path/to/the/flac\_files/big\_hit.flac

your ecommerce sends an header:

X-Accel-Redirect: /path/to/the/flac\_files/big\_hit.flac

when Nginx sees this he knows that now the file is to be served  
directly from the file system as a regular static file. Note that the  
real location is hidden from the client.

Note that the real location must be protected from direct access by  
the client always, be it from your app, be it from your server, for  
example using the internal keyword:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#internal)**
>
> This directive appeared in version 1.11.8.

In a nutshell that’s how it works.

HTH,  
— appa

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [October 2, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/5 "2012-10-02T14:01:57Z")

</div>

## António P. P. Almeida Wrote:

> when Nginx sees this he knows that now the file is to be served  
> directly from the file system as a regular static file. Note that the  
> real location is hidden from the client.
> 
> Note that the real location must be protected from direct access by  
> the client always, be it from your app, be it from your server, for  
> example using the internal keyword:
> 
> [Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#internal)
> 
> In a nutshell that’s how it works.

I ended up using nginx as a reverse proxy, although this is what I  
wanted to  
avoid in the first place (bandwidth reasons).  
Main traffic passes through nginx which forwards it to apache by  
proxy\_pass,  
and apache sends back the X-Accel-Redirect and nginx responds with the  
file  
(protected with internal; ).

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,231059,231311#msg-231311](http://forum.nginx.org/read.php?2,231059,231311#msg-231311)

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [October 2, 2012, 2:20pm UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/6 "2012-10-02T14:20:41Z")

</div>

On 2 Out 2012 14h01 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > protected and for speeding up the delivery your application sends
> > 
> > [Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#internal)
> > 
> > In a nutshell that’s how it works.
> 
> I ended up using nginx as a reverse proxy, although this is what I  
> wanted to avoid in the first place (bandwidth reasons). Main  
> traffic passes through nginx which forwards it to apache by  
> proxy\_pass, and apache sends back the X-Accel-Redirect and nginx  
> responds with the file (protected with internal; ).

Instead of Apache you can make your app return that header. For  
example, on drupal there’s a module that sends that header for  
protected file systems.

— appa

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [September 26, 2012, 11:11am UTC](https://www.ruby-forum.com/t/x-accel-redirect-testing/223207/7 "2012-09-26T11:11:14Z")

</div>

On Sep 26, 2012, at 1:03 PM, Antnio P. P. Almeida wrote:

> > Xsendfile?  
> > Nginx as a regular static file.
> 
> In a nutshell that’s how it works.  
> Some big users are even doing fancy “hybrid cloud” with nginx this way  
> 🙂

In such scenarios they might have an application on premises and the  
content on Amazon S3 (or vice versa). Instead of doing usual HTTP  
redirect to AWS and keeping another part of application on Amazon to  
retrieve and serve content to the clients, such installations rely on  
internal redirection/secure retrieval.

Some of them chose to deploy nginx just because of how this particular  
feature works.

> HTH,  
> — appa
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
AA @ nginx

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
