# Strange problem with cache

**URL:** <https://www.ruby-forum.com/t/strange-problem-with-cache/223992>\
**Category:** NGINX\
**Created:** [October 25, 2012, 10:35pm UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992 "2012-10-25T22:35:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 25, 2012, 10:35pm UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/1 "2012-10-25T22:35:00Z")

</div>

Hi,

i’ve come accross a strange problem of caching. My site is run by nginx  
1.2.1, on two different servers. The config are exactly the same (i use  
puppet to mirror configs). The contents are exactly the same, everything  
that i can think of is exactly identical on both servers.

In the config, i set “expires max” for the /static location.

The two nginx are behind a haproxy instance for loadbalancing and  
failover.  
The setup is in active-active mode.

When i load a page with no browser cache, each item in the static  
location  
is served by nginx with the correct response regarding the max-age and  
expire directives. Then i reload the page : some of these ressources are  
served with a 304 response (that is the expected behaviour), some with a  
200  
code (hence, a fresh copy of the file). If i reload the page again and  
again, the ressources served with 304 answer are varying.

Now, i’ve put the server into active-backup mode, and only one instance  
of  
nginx is serving the files. In this setup, the ressources are always  
served  
with a 304 answer.

Did i miss something? I assume that my servers setup is quite common,  
and i  
d’ont believe that the behaviour i observe is “normal”.

Any idea?

thanks

Florian

Posted at Nginx Forum:

> **[Strange problem with cache](https://forum.nginx.org/read.php?2,232235,232235#msg-232235)**
>
> Hi, i've come accross a strange problem of caching. My site is run by nginx 1.2.1, on two different servers. The config are exactly the same (i use puppet to mirror configs). The contents are exactly the same, everything that i can think of is...

---

<div class="post-metadata">

**Author:** ![florian.iragne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb9ed0/32.png) [@florian.iragne](https://www.ruby-forum.com/u/florian.iragne)\
**Post date:** [October 26, 2012, 12:42pm UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/2 "2012-10-26T12:42:06Z")

</div>

Hello!

On Thu, Oct 25, 2012 at 04:34:14PM -0400, florian.iragne wrote:

> The setup is in active-active mode.  
> with a 304 answer.
> 
> Did i miss something? I assume that my servers setup is quite common, and i  
> d’ont believe that the behaviour i observe is “normal”.
> 
> Any idea?

Symptoms suggest last modification times are different for the  
same static file on different servers.

You have to sync files between servers with modification time  
preserved for conditional requests to work correctly (“rsync -a”  
is usually a good starting point).

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![florian.iragne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb9ed0/32.png) [@florian.iragne](https://www.ruby-forum.com/u/florian.iragne)\
**Post date:** [October 26, 2012, 1:57pm UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/3 "2012-10-26T13:57:27Z")

</div>

Ok, now i understand. The browser sends a request withe the  
if-not-modified-since corresponding to one server to the other server,  
and  
this one reply with 200 since the mtime is in the future of the  
if-not-modified-since

is there any simple solution to handle this? I don’t want to rsync  
assets  
between the servers

I use django in my project and its static/collectstatic process. So, i  
force  
the mtime of each static file collected, afet each collecstatic  
operation. I  
give the command line if someone fall in the same trap:  
./manage.py collectstatic --noinput && find static -type f -exec touch  
-t  
$(date +%m%d%H)00 ‘{}’ ;

Now, the cache is working as intended and my servers are in  
active-active  
setup.

Thanks for your answer that leads me to the solution

Posted at Nginx Forum:

> **[Re: Strange problem with cache](https://forum.nginx.org/read.php?2,232235,232254#msg-232254)**
>
> Hi, i've come accross a strange problem of caching. My site is run by nginx 1.2.1, on two different servers. The config are exactly the same (i use puppet to mirror configs). The contents are exactly the same, everything that i can think of is...

---

<div class="post-metadata">

**Author:** ![florian.iragne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb9ed0/32.png) [@florian.iragne](https://www.ruby-forum.com/u/florian.iragne)\
**Post date:** [October 27, 2012, 2:36pm UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/4 "2012-10-27T14:36:33Z")

</div>

Yesterday Oct 26, 2012 at 07:57 florian.iragne wrote:

> Ok, now i understand. The browser sends a request withe the  
> if-not-modified-since corresponding to one server to the other server, and  
> this one reply with 200 since the mtime is in the future of the  
> if-not-modified-since
> 
> is there any simple solution to handle this? I don’t want to rsync assets  
> between the servers

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#if_modified_since)**
>
> This directive appeared in version 1.11.8.

if\_modified\_since before;

–  
WNGS-RIPE

---

<div class="post-metadata">

**Author:** ![florian.iragne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb9ed0/32.png) [@florian.iragne](https://www.ruby-forum.com/u/florian.iragne)\
**Post date:** [October 29, 2012, 9:00am UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/5 "2012-10-29T09:00:25Z")

</div>

Thanks for your answer. However, it’s not secure enough for me and i  
prefer  
adding one more command line to the deployment process to ensure cache  
is  
working.

thanks

Posted at Nginx Forum:

> **[Re: Strange problem with cache](https://forum.nginx.org/read.php?2,232235,232297#msg-232297)**
>
> Hi, i've come accross a strange problem of caching. My site is run by nginx 1.2.1, on two different servers. The config are exactly the same (i use puppet to mirror configs). The contents are exactly the same, everything that i can think of is...

---

<div class="post-metadata">

**Author:** ![florian.iragne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/eb9ed0/32.png) [@florian.iragne](https://www.ruby-forum.com/u/florian.iragne)\
**Post date:** [October 29, 2012, 9:39am UTC](https://www.ruby-forum.com/t/strange-problem-with-cache/223992/6 "2012-10-29T09:39:36Z")

</div>

Hello!

On Sat, Oct 27, 2012 at 03:35:51PM +0300, Oleksandr V. Typlyns’kyi  
wrote:

> [http://nginx.org/r/if\_modified\_since](http://nginx.org/r/if_modified_since)  
> if\_modified\_since before;

1. This will work only if you’ll switch off ETag’s as well.

2. This isn’t a solution, but a bandaid. It won’t fix the real  
problem (mtimes mismatch), but will make response with greatest  
mtime eventually win. Even with only 2 servers used this  
will still result in about 1.5x more 200 responses than needed.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
