# How can i set diffrent proxy cache time by diffrent uri

**URL:** <https://www.ruby-forum.com/t/how-can-i-set-diffrent-proxy-cache-time-by-diffrent-uri/244066>\
**Category:** NGINX\
**Created:** [February 15, 2016, 11:12am UTC](https://www.ruby-forum.com/t/how-can-i-set-diffrent-proxy-cache-time-by-diffrent-uri/244066 "2016-02-15T11:12:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [February 15, 2016, 11:12am UTC](https://www.ruby-forum.com/t/how-can-i-set-diffrent-proxy-cache-time-by-diffrent-uri/244066/1 "2016-02-15T11:12:12Z")

</div>

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?

Posted at Nginx Forum:

> **[how can i set diffrent proxy cache time by diffrent uri](https://forum.nginx.org/read.php?2,264539,264539#msg-264539)**
>
> 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; }

---

<div class="post-metadata">

**Author:** ![vps4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/76d3ee/32.png) [@vps4](https://www.ruby-forum.com/u/vps4)\
**Post date:** [February 15, 2016, 2:10pm UTC](https://www.ruby-forum.com/t/how-can-i-set-diffrent-proxy-cache-time-by-diffrent-uri/244066/2 "2016-02-15T14:10:58Z")

</div>

Hello!

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

> ```
> if ($request_uri ~ "^/other") {
> 
> ```
> 
> }
> 
> 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 D.

> **[nginx](http://nginx.org/)**
