Possible to modify response headers from a proxied request before the response is written do the cache? (modified headers should be written to disk)

Maxim Dounin mdounin at mdounin.ru
Wed Mar 13 02:10:32 UTC 2019


Hello!

On Tue, Mar 12, 2019 at 09:42:22PM +0100, Manuel wrote:

> > From practical point of view, however, these should be enough to
> > return correct responses to clients.  What is stored in the cache
> > file is irrelevant.
> 
> Well, the expires header is in the cached file, and that was the problem.
> The expires was not 30d but some 1.x days.
> And so the cache will request upstream to early, because upstream
> returned Cache-Control: max-age=126195
> 
> I want to cache the upstream resource for 30d
> regardless of the returned cache headers from upstream.

As long as a particular header is ignored using the 
"proxy_ignore_header" directive, it doesn't matter what it 
contains in the response returned by upstream server and/or in the 
cache file, nginx will not use the header to determine response 
validity time.  Instead, it will use other headers which are not 
ignored (if any), or will determine cache validity based on 
proxy_cache_valid directives.  The resulting validity time is 
stored in the binary cache header which comes before response 
headers in the cache file.

Note well that cache validity time is determined when a response 
is stored into cache, so changing proxy_cache_valid in the 
configuration won't change the validity times of previously cached 
responses.  A response needs to be re-cached for proxy_cache_valid 
changes to be applied.

> My solution now is a two step approach:
> step one: check cache, if the resource is expired
> or not cached, nginx calls itself to get the resource.
> Step two: call upstream and modify the expires
> header to 30d. Return response to the cache.
> Cache is now happy with an expires 30d header :-)

Well, using double proxying will certainly work too, but in this 
particular case it is not needed.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list