how can i set diffrent proxy cache time by diffrent uri

Maxim Dounin mdounin at mdounin.ru
Mon Feb 15 13:10:23 UTC 2016


Hello!

On Mon, Feb 15, 2016 at 05:11:40AM -0500, vps4 wrote:

> i tried to use like this
> 
> server {
> 
>     set $cache_time 1d;
> 
>     if ($request_uri = "/") {
>         set $cache_time 3d;
>     }
> 
>     if ($request_uri ~ "^/other") {
>         set $cache_time 30d;
>     }
> 
>     location / {
>         try_files $uri @fetch;
>     }
> 
>     location @fetch {
>          proxy_cache_valid  200 301 302 $cache_time;
>     }
> }
> 
> then i got error "invalid time value "$cache_time" in /etc/nginx/xxx.conf
> 
> how can i fix this?

Variables are not supported by the proxy_cache_valid directive.  
Use different locations instead, e.g.:

    location / {
        try_files $uri @fetch;
    }

    location /other {
        try_files $uri @fetch_other;
    }

    location @fetch {
        proxy_cache_valid 200 301 302 3d;
        ...
    }

    location @fetch_other {
        proxy_cache_valid 200 301 302 30d;
        ...
    }

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list