using $upstream* variables inside map directive

Ruslan Ermilov ru at nginx.com
Wed May 7 14:39:48 UTC 2014


On Wed, May 07, 2014 at 08:53:56AM -0400, Kirill K. wrote:
> Thanks, Ruslan,
> Thing is, I tried to "debug" whether $dontcache is being set at all by
> exposing it via response headers (along with content-length), and it shows
> that $upstream_response_length is ignored by map completely, i.e. no matter
> where I use $dontcache, it will never get any value different from default
> (i.e. 0). Even though $upstream_response_length  is validated correctly (and
> can be exposed in headers), the map directive just ignores it.

I tested that your map works when used ONLY in proxy_no_cache,
and it indeed DTRT: responses with content length less than
100 aren't cached.

Here's the config snippet:

: http {
:     proxy_cache_path proxy_cache keys_zone=proxy_cache:10m;
: 
:     map $upstream_http_content_length $dontcache {
:         default 0;
:         ~^\d\d$ 1;
:         ~^\d$ 1;
:     }
: 
:     server {
:         listen 8000;
: 
:         location / {
:             proxy_pass http://127.0.0.1:8001;
:             proxy_cache proxy_cache;
:             proxy_cache_valid 300m;
:             proxy_no_cache $dontcache;
: 
:             add_header X-DontCache $dontcache;
:         }
:     }
: 
:     server {
:         listen 8001;
: 
:         return 200 "ok";
:     }
: }

$ curl -i http://127.0.0.1:8000/test
HTTP/1.1 200 OK
Server: nginx/1.7.1
Date: Wed, 07 May 2014 14:34:19 GMT
Content-Type: text/plain
Content-Length: 2
Connection: keep-alive
X-DontCache: 1

ok

And there will be no file in "proxy_cache" directory.



More information about the nginx mailing list