using $upstream* variables inside map directive

Jonathan Matthews contact at
Wed May 7 10:31:10 UTC 2014

On 7 May 2014 06:38, Kirill K. <nginx-forum at> wrote:
> Hello,
> I'm trying to avoid caching of small responses from upstreams using map:
> map $upstream_http_content_length $dontcache {
> default 0;
> ~^\d\d$ 1;
> ~^\d$ 1;
> }
> Unfortunatelly, nginx seems to ignore $upstream* variables at the map
> processing stage, hence variables like $upstream_http_content_length or
> $upstream_response_length stay empty when map directive is processed (this
> can be observed in debug log as "http map started" message). In case I use
> non-upstream related variables, a map works as expected.
> Question: is there any way to use $upstream* vars inside the map directive,
> or maybe someone can offer alternative way to detect small upstream response
> in order to bypass cache?

I don't explicitly know how to achieve what you're trying to, but I
seem to recall mention on this list that a map's value gets stuck
(per-request) the first time it's evaluated. I might be
misremembering, but this does ring a bell.

So - is your map somehow being evaluated /before/ the upstream vars
are available? Does your config perhaps cause it to be evaluated when
the initial request arrives, to see if the response should be served
from cache; then the request is proxy_pass'd, then after receiving a
response the caching bypass config is examined but to no avail as the
map has "stuck" with the initially set value?

Sorry I can't be more specific - I'm sure others can help more definitively!


More information about the nginx mailing list