# Nginx, static files and local caching

**URL:** <https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233>\
**Category:** NGINX\
**Created:** [February 10, 2010, 10:21am UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233 "2010-02-10T10:21:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 10:21am UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/1 "2010-02-10T10:21:44Z")

</div>

I’m currently using nginx for my static files.  
Apache is still being used to serve my dynamic PHP scripts.  
This configuration works perfectly

Is it possible to cache the static files served bij nginx?  
Because I’m using NFS, and when the files aren’t cache my NFS servers  
gets a very high load.

I’ve found something with try\_files, but I don’t think that works  
because it looks like it uses Apache to serve the static files.  
I searched all forums but I can’t found a good solution.

Who can help me?

Posted at Nginx Forum:

> **[Nginx, static files and local caching](https://forum.nginx.org/read.php?2,52985,52985#msg-52985)**
>
> I'm currently using nginx for my static files. Apache is still being used to serve my dynamic PHP scripts. This configuration works perfectly Is it possible to cache the static files served bij nginx? Because I'm using NFS, and when the files aren't...

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 10:55am UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/2 "2010-02-10T10:55:41Z")

</div>

make apache serve static pages and cache them with proxy\_cache in nginx.

2010/2/10 rkappert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/3 "2010-02-10T16:14:06Z")

</div>

> Is it possible to cache the static files served bij nginx?  
> Because I’m using NFS, and when the files aren’t cache my NFS servers gets  
> a very high load.

ngx\_slowfs\_cache was created exactly to solve this problem:

> **[FRiCKLE Labs / nginx / ngx\_slowfs\_cache](http://labs.frickle.com/nginx_ngx_slowfs_cache)**

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/4 "2010-02-10T18:31:04Z")

</div>

> Shouldn’t NFS be caching already?

Should it? It’s remote location which can be modified by other systems.

> Given you have free ram available on the  
> nginx machine, it should be caching the files in disk cache. Why the  
> redundancy?

Well, for one, free local disk space is probably order of magnitude  
bigger  
than buffer cache.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/5 "2010-02-10T17:02:41Z")

</div>

Shouldn’t NFS be caching already? Given you have free ram available on  
the  
nginx machine, it should be caching the files in disk cache. Why the  
redundancy?

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/6 "2010-02-10T22:00:40Z")

</div>

On Wed, Feb 10, 2010 at 12:29 PM, Piotr S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Shouldn’t NFS be caching already?
> 
> > 
> 
> Should it? It’s remote location which can be modified by other systems.

Yes it should. If it isn’t your implementation is not working properly.

> Given you have free ram available on the
> 
> > nginx machine, it should be caching the files in disk cache. Why the  
> > redundancy?
> 
> Well, for one, free local disk space is probably order of magnitude bigger  
> than buffer cache.

I can’t disagree with this, It’s use as a remote to local filesystem  
cache  
would be useful. Does it have stateful checks to determine if files are  
changed?

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 11, 2010, 6:57am UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/7 "2010-02-11T06:57:26Z")

</div>

Hi Piotr,

I tried this module, but it seems the /tmp/nginx folder is always empty.  
Is  
there any i’ve missed?

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 10, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/8 "2010-02-10T22:18:14Z")

</div>

> Yes it should.

NFS cache is/was broken, so it’s open discussion, but not exactly on  
topic.

> If it isn’t your implementation is not working properly.

ngx\_slowfs\_cache isn’t NFS implementation, it’s caching mechanism.

> I can’t disagree with this, It’s use as a remote to local filesystem cache  
> would be useful. Does it have stateful checks to determine if files are  
> changed?

Like I said on IRC: it’s cache by nginx definition (make local copy and  
serve it unconditionally until it expires), so no, it doesn’t check if  
original file was modified.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 12, 2010, 4:59am UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/9 "2010-02-12T04:59:50Z")

</div>

Here’s my config

http {

slowfs\_cache\_path /data/cache levels=1:2 keys\_zone=fastcache:10m;  
slowfs\_temp\_path /data/temp 1 2;

…

server {  
listen 80;  
root /var/www;

```
    location / {
        slowfs_cache fastcache;
        slowfs_cache_key $uri;
        slowfs_cache_valid 1d;
    }

    location ~ /purge(/.*) {
        allow 127.0.0.1;
        deny all;
        slowfs_cache_purge fastcache $1;
    }

```

}

}

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 12, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/10 "2010-02-12T22:10:15Z")

</div>

Sorry my mistake… please ignore the previous email… it seems to be  
working  
🙂

Is there any way I can if user is accessing my cache file?

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 12, 2010, 11:15pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/11 "2010-02-12T23:15:48Z")

</div>

> Is there any way I can if user is accessing my cache file?

Can you see it in logs?  
Yes, you can use $slowfs\_cache\_status for that.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![rkappert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aeb1de/32.png) [@rkappert](https://www.ruby-forum.com/u/rkappert)\
**Post date:** [February 11, 2010, 4:20pm UTC](https://www.ruby-forum.com/t/nginx-static-files-and-local-caching/183233/12 "2010-02-11T16:20:34Z")

</div>

Hi,

> I tried this module, but it seems the /tmp/nginx folder is always empty.  
> Is  
> there any i’ve missed?

I can’t really answer that question without you posting your  
configuration.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>
