Internal, redirects and proxy_cache

A very related question was asked before here:

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

Hello!

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

A very related question was asked before here:

Re: proxy_cache and internal redirect

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).


Maxim D.
http://nginx.org/en/donation.html