proxy_cache and variable

Ekaterina Kukushkina ek at nginx.com
Thu Oct 29 06:55:32 UTC 2015


Hello trungaczne,

In your case this is expected behavior. This is because the 
$upstream_http_content_type exist only when the upstream already started to
return response to the nginx box. But nginx should determine the caching
policy for current request before any connection to the upstream will be
performed. Thus, the $should_not_cache variable has always set to the default
value.

If you want to change cache policy according to the file type, you have to
use variables which exist before asking upstream. For example, $uri.

 

> On 29 Oct 2015, at 08:41, trungaczne <nginx-forum at nginx.us> wrote:
> 
> Using "proxy_cache" directive with a variable seems to change that
> variable's value.
> 
> Tested against a fresh install of nginx 1.9.6 (default configure parameters)
> on 2 different Ubuntu:14.04 hosts. DigitalOcean droplets.
> 
> The following configuration works as expected: It returns 0/1/2 depending on
> the mimetype returned by the upstream. The returned header is always
> correct.
> 
> worker_processes  1;
> 
> events {
>    worker_connections  1024;
> }
> 
> 
> http {
>    include       mime.types;
>    default_type  application/octet-stream;
> 
>    sendfile        on;
> 
>    keepalive_timeout  65;
> 
>    map $upstream_http_content_type $should_not_cache {
>        default        1;
>        "~image/"      2;
>        "~css"         0;
>    }
>    add_header X-Should-Not-Cache $should_not_cache;
> 
>    server {
>        listen       80;
>        server_name  myupstream.com;
> 
>        location / {
>            proxy_pass   http://<upstreamip>;
>            proxy_set_header Host $host;
> 
>        }
>    }
> 
> }
> 
> This on the other hand always return the default value of the "map"
> directive (X-Should-Not-Cache always return 1 regardless of whether it's
> css/image/etc):
> 
> worker_processes  1;
> 
> events {
>    worker_connections  1024;
> }
> 
> 
> http {
>    include       mime.types;
>    default_type  application/octet-stream;
> 
>    sendfile        on;
> 
>    keepalive_timeout  65;
> 
>    map $upstream_http_content_type $should_not_cache {
>        default        1;
>        "~image/"      2;
>        "~css"         0;
>    }
> 
>    proxy_cache_path  /tmp/low levels=1:2  keys_zone=0:1m  inactive=1m 
> max_size=1g;
>    proxy_cache_path  /tmp/medium levels=1:2  keys_zone=1:1m  inactive=1m 
> max_size=1g;
>    proxy_cache_path  /tmp/high levels=1:2  keys_zone=2:10m  inactive=10m 
> max_size=1g;
>    proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
>    proxy_cache_methods GET HEAD;
>    proxy_cache_valid 200 302 10m;
>    proxy_cache_valid 404      1m;
>    proxy_cache $should_not_cache;
> 
>    add_header X-Should-Not-Cache $should_not_cache;
> 
>    server {
>        listen       80;
>        server_name  myupstream.com;
> 
>        location / {
>            proxy_pass   http://<upstreamip>;
>            proxy_set_header Host $host;
> 
>        }
>    }
> 
> }
> 
> (basically I'm trying to have different cache zones for different mime types
> with different time parameters)
> 
> Also, in the second config, commenting out the "proxy_cache" directive makes
> the header correct again.
> 
> Can anybody explain this behavior?
> 
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,262527,262527#msg-262527
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
> 

--
Ekaterina Kukushkina



More information about the nginx mailing list