# Nginx Cache Valid till - Help

**URL:** <https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077>\
**Category:** NGINX\
**Created:** [November 14, 2010, 6:12am UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077 "2010-11-14T06:12:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 14, 2010, 6:12am UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077/1 "2010-11-14T06:12:41Z")

</div>

Hi,

I have a issue my nginx cofig is working ok just i want cache to clear  
only once in 15 days.  
But it is clearing everyday and new images showing on homepage ( we use  
random images hence on each refresh its new)  
however with nginx it needs to show same page and images etc picked from  
cache.  
It works ok for 24 hours then again refreshes all content…as if cache  
is clear.

## Here is Nginx conf

user nginx;  
worker\_processes 16;  
worker\_rlimit\_nofile 100000;

error\_log /var/log/nginx/error.log crit;  
#error\_log /var/log/nginx/error.log notice;  
#error\_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

events {  
worker\_connections 102400;  
use epoll;  
}

http {  
proxy\_cache\_path /var/lib/nginx/tmp/cache1 levels=1:2  
keys\_zone=proxy\_cache1:16m max\_size=20g inactive=1d;  
proxy\_cache\_path /var/lib/nginx/tmp/cache2 levels=1:2  
keys\_zone=proxy\_cache2:16m max\_size=40g inactive=30d;  
proxy\_temp\_path /var/lib/nginx/tmp/proxy;

```
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local]

```

“$request” ’  
'$status $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

```
access_log off;# /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
keepalive_timeout 65;

```

* * *

## Here is cache inc

access\_log off;  
location / {  
proxy\_pass [http://96.30.58.176:80](http://96.30.58.176:80);  
include /etc/nginx/generic.inc;  
proxy\_ignore\_headers “Cache-Control” “Expires”;  
proxy\_cache\_key  
“$server\_name$scheme$proxy\_host$uri$is\_args$args$cookie\_sid”;  
proxy\_cache proxy\_cache1;  
proxy\_cache\_use\_stale updating error timeout invalid\_header  
http\_500;  
proxy\_cache\_valid 15d;  
}

```
location ~*

```

## ^.+.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar)$ { proxy\_pass [http://96.30.58.176:80](http://96.30.58.176:80); include /etc/nginx/generic.inc; proxy\_ignore\_headers “Cache-Control” “Expires”; proxy\_cache\_key “$server\_addr:$server\_port$request\_uri”; proxy\_cache proxy\_cache2; proxy\_cache\_use\_stale updating error timeout invalid\_header http\_500; proxy\_cache\_valid 15d; }

## Here is site.conf in called conf.d called

server {  
listen 96.30.58.176:8080;  
server\_name [www.site.com](http://www.site.com);  
include /etc/nginx/cache.inc;  
location /nginx\_status {  
stub\_status on;  
}  
if ($http\_host ~\* “^site.com$”){  
set $rule\_0 1$rule\_0;  
}  
if ($rule\_0 = “1”){  
rewrite ^/(.\*)$ [Custom Application Development Software for Business - Salesforce.com](http://www.site.com/$1) permanent;  
break;  
}  
}

Regards

Posted at Nginx Forum:

> **[Nginx Cache Valid till - Help](https://forum.nginx.org/read.php?2,150639,150639#msg-150639)**
>
> Hi, I have a issue my nginx cofig is working ok just i want cache to clear only once in 15 days. But it is clearing everyday and new images showing on homepage ( we use random images hence on each refresh its new) however with nginx it needs to show...

---

<div class="post-metadata">

**Author:** ![rajivv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rajivv](https://www.ruby-forum.com/u/rajivv)\
**Post date:** [November 14, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077/2 "2010-11-14T14:19:55Z")

</div>

Have you tried setting the following in your location blocks?

```auto
expires 15d;
```

Posted at Nginx Forum:

> **[Re: Nginx Cache Valid till - Help](https://forum.nginx.org/read.php?2,150639,150688#msg-150688)**
>
> Hi, I have a issue my nginx cofig is working ok just i want cache to clear only once in 15 days. But it is clearing everyday and new images showing on homepage ( we use random images hence on each refresh its new) however with nginx it needs to show...

---

<div class="post-metadata">

**Author:** ![rajivv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rajivv](https://www.ruby-forum.com/u/rajivv)\
**Post date:** [November 14, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077/3 "2010-11-14T17:35:39Z")

</div>

Hello!

On Sun, Nov 14, 2010 at 12:10:59AM -0500, rajivv wrote:

> I have a issue my nginx cofig is working ok just i want cache to clear  
> only once in 15 days.  
> But it is clearing everyday and new images showing on homepage ( we use  
> random images hence on each refresh its new)  
> however with nginx it needs to show same page and images etc picked from  
> cache.  
> It works ok for 24 hours then again refreshes all content…as if cache  
> is clear.

I guess by “random images” you mean random ![]() in your  
html code, right?

[…]

> http {  
> proxy\_cache\_path /var/lib/nginx/tmp/cache1 levels=1:2  
> keys\_zone=proxy\_cache1:16m max\_size=20g inactive=1d;  
> proxy\_cache\_path /var/lib/nginx/tmp/cache2 levels=1:2  
> keys\_zone=proxy\_cache2:16m max\_size=40g inactive=30d;  
> proxy\_temp\_path /var/lib/nginx/tmp/proxy;

[…]

> ```
> location / {
> 
> ```

[…]

> ```
> proxy_cache_key
> 
> ```
> 
> “$server\_name$scheme$proxy\_host$uri$is\_args$args$cookie\_sid”;  
> proxy\_cache proxy\_cache1;

Note that your cache key for html contains $cookie\_sid. It looks  
like session cookie, and probably per-user and likely to expire  
somewhere earlier than after 15 days.

Also note that it uses proxy\_cache1 which has 1d inactivity  
timeout, i.e. if no requests for a given key (with $cookie\_sid)  
seen by nginx within 1d cache entry will be removed.

Maxim D.

---

<div class="post-metadata">

**Author:** ![rajivv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rajivv](https://www.ruby-forum.com/u/rajivv)\
**Post date:** [November 15, 2010, 4:10am UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077/4 "2010-11-15T04:10:52Z")

</div>

I will try inactive at 15 days and see do i need to restart nginx…

Posted at Nginx Forum:

> **[Re: Nginx Cache Valid till - Help](https://forum.nginx.org/read.php?2,150639,150815#msg-150815)**
>
> Hi, I have a issue my nginx cofig is working ok just i want cache to clear only once in 15 days. But it is clearing everyday and new images showing on homepage ( we use random images hence on each refresh its new) however with nginx it needs to show...

---

<div class="post-metadata">

**Author:** ![rajivv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@rajivv](https://www.ruby-forum.com/u/rajivv)\
**Post date:** [November 15, 2010, 4:30am UTC](https://www.ruby-forum.com/t/nginx-cache-valid-till-help/199077/5 "2010-11-15T04:30:21Z")

</div>

What to do about session cookie ?

Posted at Nginx Forum:

> **[Re: Nginx Cache Valid till - Help](https://forum.nginx.org/read.php?2,150639,150820#msg-150820)**
>
> Hi, I have a issue my nginx cofig is working ok just i want cache to clear only once in 15 days. But it is clearing everyday and new images showing on homepage ( we use random images hence on each refresh its new) however with nginx it needs to show...
