Priority of cache-control, expires, x-accel-expires

Maxim Dounin mdounin at mdounin.ru
Wed Jan 19 13:52:04 UTC 2022


Hello!

On Tue, Jan 18, 2022 at 09:19:09PM -0500, yugo-horie wrote:

> Excuse me for refer to quite old issue, we found a different behavior
> regarding their headers order in case of X-Accel-Expires which is not 0 and
> Cache-Control has any of no-store, no-cache or private.
> 
> It is very easy reproducing process.
> 
> ngx_http_upstream.c has two method ngx_http_upstream_process_cache_control
> and ngx_http_upstream_process_accel_expires which operations their cache
> related headers, Cache-Control is processed in
> ngx_http_upstream_process_cache_control, X-Accel-Expires is processed in
> process_accel_expires.
> 
> ngx_http_upstream_process_cache_control in ngx_http_upstream.c 
> 
> 4738     if (r->cache->valid_sec != 0 && u->headers_in.x_accel_expires !=
> NULL) {
> 4739         return NGX_OK;
> 4740     }
> 4741
> 4742     start = h->value.data;
> 4743     last = start + h->value.len;
> 4744
> 4745     if (ngx_strlcasestrn(start, last, (u_char *) "no-cache", 8 - 1) !=
> NULL
> 4746         || ngx_strlcasestrn(start, last, (u_char *) "no-store", 8 - 1)
> != NULL
> 4747         || ngx_strlcasestrn(start, last, (u_char *) "private", 7 - 1)
> != NULL)
> 4748     {
> 4749         u->cacheable = 0;
> 4750         return NGX_OK;
> 4751     }
> 
> 
> If Cache-Control from upstream has  a value of no-store, no-cache or
> private, u->cacheable = 0; in ngx_http_upstream_process_cache_control
> 
> In the case of before processing ngx_http_upstream_process_accel_expires, if
> it sets u->cacheable = 0 for this procedure, it does not cache according to
> Cache-Control. X-Accel-Expires is only overwriting valid_sec.
> 
> OTOH, In the case of after processing
> ngx_http_upstream_process_accel_expires,
> ngx_http_upstream_process_cache_control returns NGX_OK earlier than the
> procedure of Cache-Control: no-xxx or private. 
> In this case, it does cache, the cache is following x-accel-expires values.
> It ignores Cache-Control.
> 
> As this result it seems not to override entirely Cache-Control by
> X-Accel-Expires. And It has differential behavor according to order of
> upstream header. We could not find this behavior in any nginx documents.
> Could you tell me what is true?

Your analysis is correct, X-Accel-Expires only takes precedence 
for cache validity time.  Other flags, such as non-cacheable 
status and stale-if-error/stale-while-revalidate times might be 
used from the Cache-Control header if it comes first.

Certainly this is a misfeature, and patches to address this are 
welcome.

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



More information about the nginx mailing list