# Internal, redirects and proxy\_cache

**URL:** <https://www.ruby-forum.com/t/internal-redirects-and-proxy-cache/245619>\
**Category:** NGINX\
**Created:** [June 22, 2013, 11:11am UTC](https://www.ruby-forum.com/t/internal-redirects-and-proxy-cache/245619 "2013-06-22T11:11:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [June 22, 2013, 11:11am UTC](https://www.ruby-forum.com/t/internal-redirects-and-proxy-cache/245619/1 "2013-06-22T11:11:04Z")

</div>

A very related question was asked before here:

> **[Re: proxy\_cache and internal redirect](https://forum.nginx.org/read.php?2,236720,236782#msg-236782)**
>
> Hello. Can you please tell how to enable caching of responses with 'X-Accel-Redirect' headers. Nick. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ nginx mailing list nginx@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx

But I’m not sure that asked exactly what I want to do.

I’ve got locations like this:

```
location /packages/archive-contents {
    proxy_pass http://localhost:8005;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /packages/archive-contents/ {
    internal;
    proxy_pass http://localhost:8005;
    proxy_http_version 1.1;
}

```

The first one calls out to the app server which sends an X-Forwarded-For  
to the second one, something like:

```
http://host/packages/archive-contents/38381632639138

```

I would like to cache that response so that nginx hit the app server  
from the first location which redirects to the 2nd location but that  
returns a cached response.

When the redirect from the first location changes the second location  
would have to cache it.

Is that possible? I’ve tried adding proxy\_cache to the 2nd location,  
like so:

```
location /packages/archive-contents/ {
    internal;
    proxy_pass http://localhost:8005;
    proxy_http_version 1.1;
    proxy_cache marmaladerepo-cache;
    proxy_cache_valid 200 302 1d;
    proxy_cache_valid 404 60m;
}

```

but this doesn’t seem to have an effect.

Nic Ferrier

---

<div class="post-metadata">

**Author:** ![Nic\_Ferrier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@Nic\_Ferrier](https://www.ruby-forum.com/u/Nic_Ferrier)\
**Post date:** [June 22, 2013, 11:48am UTC](https://www.ruby-forum.com/t/internal-redirects-and-proxy-cache/245619/2 "2013-06-22T11:48:28Z")

</div>

Hello!

On Sat, Jun 22, 2013 at 10:10:25AM +0100, Nic Ferrier wrote:

> A very related question was asked before here:
> 
> [http://forum.nginx.org/read.php?2,236720,236782#msg-236782](http://forum.nginx.org/read.php?2,236720,236782#msg-236782)
> 
> But I’m not sure that asked exactly what I want to do.

It’s not. It’s about caching of a response with  
an X-Accel-Redirect header, while your need to cache a response  
after an internal redirect. What you need is expected to work  
fine, see below.

> ```
> proxy_pass http://localhost:8005;
> proxy_http_version 1.1;
> }
> 
> ```
> 
> The first one calls out to the app server which sends an X-Forwarded-For  
> to the second one, something like:
> 
> ```
> http://host/packages/archive-contents/38381632639138
> 
> ```

Correct header name is X-Accel-Redirect. Correct value is  
“/packages/archive-contents/38381632639138”. That is, header  
returned should look like:

X-Accel-Redirect: /packages/archive-contents/38381632639138

> ```
> location /packages/archive-contents/ {
> internal;
> proxy_pass http://localhost:8005;
> proxy_http_version 1.1;
> proxy_cache marmaladerepo-cache;
> proxy_cache_valid 200 302 1d;
> proxy_cache_valid 404 60m;
> }
> 
> ```
> 
> but this doesn’t seem to have an effect.

This should work assuming you are using correct header to trigger  
internal redirect, and the response returned doesn’t disable cache  
(there is more than one way to do it, see  
[Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_cache_valid)).

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**
