Empty $upstream_http_location variable if response was cached

Posted by Ivaldi on Server Fault See other posts from Server Fault or by Ivaldi
Published on 2014-06-12T09:21:24Z Indexed on 2014/06/12 9:26 UTC
Read the original article Hit count: 186

Filed under:
|
|

I would like to cache the response of an redirect. (Cache the request to some site which returns a redirect and cache the second request which returns the actual content.)

So far my config looks like this:

location = /proxy {
    error_page 301 302 307 = @redir;

    resolver 8.8.8.8;

    proxy_pass          $arg_url;
    proxy_intercept_errors on;
    proxy_cache         pcache;
    proxy_cache_key     $arg_url;
    proxy_cache_valid   200 301 302 307 1d;
    proxy_cache_min_uses  1;
    proxy_ignore_client_abort on;
    proxy_ignore_headers Set-Cookie Expires Cache-Control;
}

location @redir {
    resolver 8.8.8.8;

    # we need to assign $upstream_http_location to another var in order to use it with proxy_pass
    set $target $upstream_http_location;
    proxy_pass $target;

    proxy_cache         predirects;
    proxy_cache_key     $upstream_http_location;
    proxy_cache_valid   200 301 302 307 1d;
    proxy_cache_min_uses  1;
    proxy_ignore_headers Set-Cookie Expires Cache-Control;
}

It works for the first request or without the 30x codes for proxy_cache_valid in the /proxy part, but $target and $upstream_http_location are empty, if the response was cached. Is there a nice solution to cache both requests?

Thanks!

© Server Fault or respective owner

Related posts about nginx

Related posts about proxy