# Nginx simple caching solutions

**URL:** <https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825>\
**Category:** NGINX\
**Created:** [August 1, 2012, 6:24am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825 "2012-08-01T06:24:40Z")\
**Posts on this page:** 11\
**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:** [August 1, 2012, 6:24am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/1 "2012-08-01T06:24:40Z")

</div>

I was Googling around for some simple page caching solutions (say, cache  
pages for 5 minutes, exclude certain directories) and came across Google  
results for varnish, memcached, using nginx as a reverse proxy for  
apache,  
etc.

My Googling didn’t find a method for nginx caching itself, though my  
coffee-deficient brain thinks that might still involve using the proxy  
directives to point to itself. I dunno, hence the question.

I’m just looking for a way to speed up some dynamic pages that don’t  
have  
personalization and can basically be static for a few minutes. Dedicated  
erver has 2 gig RAM and runs nginx, php-fpm, mysql 5 and a qmail server.

Any thoughts or config example links? Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 6:33am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/2 "2012-08-01T06:33:28Z")

</div>

Have you tried the proxy\_cache?  
[http://nginx.org/en/docs/http/ngx\_http\_proxy\_module.html#proxy\_cache](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache)

2012/8/1 Ian M. Evans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 6:49am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/3 "2012-08-01T06:49:50Z")

</div>

On Wed, August 1, 2012 12:32 am, Ҧΰ wrote:

> Have you tried the proxy\_cache?  
> [Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache)

I began to look at it but my brain wasn’t seeing how to set it up for  
handling nginx caching itself. It might be safe to assume that this is  
because I have a summer cold and headache but still felt the need to  
surf  
when I couldn’t sleep. 🙂

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 3:03pm UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/4 "2012-08-01T15:03:02Z")

</div>

On 01/08/2012 4:44 AM, Mark A. wrote:

> ```
> fastcgi_cache_bypass $no_cache;
> fastcgi_no_cache $no_cache;
> fastcgi_cache_valid 200 301 10m;
> fastcgi_cache_valid 302 5m;
> fastcgi_cache_valid 404 1m;
> fastcgi_cache_use_stale error timeout invalid_header updating http_500;
> fastcgi_ignore_headers Cache-Control Expires;
> expires epoch;
> fastcgi_cache_lock on;
> 
> ```

Wow, Mark, thanks. I was doing some testing earlier tonight and a few of  
my pages were fast loading on their own but under load they just ground  
the system to a halt. So this should help.

How do I turn off the caching for a specific directory? I’ll need to do  
that on my phpmyadmin and data entry/update dirs.

Thanks again.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 3:14pm UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/5 "2012-08-01T15:14:09Z")

</div>

On 1 Ago 2012 15h02 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > fastcgi\_cache\_path /var/lib/nginx/fastcgicache levels=1:2  
> > fastcgi\_param …; and so on ) add these:  
> > Wow, Mark, thanks. I was doing some testing earlier tonight and a  
> > few of my pages were fast loading on their own but under load they  
> > just ground the system to a halt. So this should help.
> 
> How do I turn off the caching for a specific directory? I’ll need to  
> do that on my phpmyadmin and data entry/update dirs.

Add at the http level a map directive:

map $uri $no\_cache\_dirs {  
default 0;  
/phpmyadmin 1;  
/data/dir 1;  
/update/dir 1;  
}

Use the proper URIs for your setup.

Add $no\_cache\_dirs to your fastcgi\_cache\_bypass +  
fastcgi\_cache\_no\_cache directives.

— appa

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 10:45am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/6 "2012-08-01T10:45:18Z")

</div>

On Wed, 1 Aug 2012 00:24:12 -0400, “Ian M. Evans”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m just looking for a way to speed up some dynamic pages that don’t  
> have personalization and can basically be static for a few minutes.  
> Dedicated erver has 2 gig RAM and runs nginx, php-fpm, mysql 5 and a  
> qmail server.
> 
> Any thoughts or config example links?

1. create dir /var/lib/nginx/fastcgicache and make it readable by nginx

2. at the very begining of your /etc/nginx/sites-available/yoursite  
file (i.e., before the first server { … line) include these 3 lines:

3. in the block where you pass the control to php-fpm (i.e., the block  
were you put include fastcgi\_params; fastcgi\_pass …;  
fastcgi\_param …; and so on ) add these:

Done.

Regards,  
M.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 5:19pm UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/7 "2012-08-01T17:19:10Z")

</div>

I set up the cache and restarted nginx. How do I debug it? I ab tested  
the front page and it’s still slow with concurrent requests. There seems  
to be a few snippets in the cache dir, but not every php file is getting  
cached.

Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 5:43pm UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/8 "2012-08-01T17:43:43Z")

</div>

On 01/08/2012 11:27 AM, Antnio P. P. Almeida wrote:

> Use cURL. Add a header with the cache status:
> 
> ## Add a cache miss/hit status header.
> 
> add\_header X-My-Cache $upstream\_cache\_status;
> 
> to your cache config. Check for this header when doing requests with cURL.

Thanks. I forgot I was sending out a cookie on the test page. Got rid of  
that and the page was cached.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 18, 2012, 7:10am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/9 "2012-08-18T07:10:32Z")

</div>

Just an update on my situation…

I installed the fastcgi caching as per the suggestions in this thread.  
Using the add\_header X-My-Cache $upstream\_cache\_status; line I’m able to  
confirm by the headers if I’m getting cache hits or misses.

I’ve also got a small handful of directories that I don’t want cached  
and as per the thread, they’re marked:

map $uri $no\_cache\_dirs {  
default 0;  
/dir1 1;  
/dir2 1;  
/dir3 1;  
/dir4 1;  
/dir5 1;  
}

In my fastcgi.conf there are the following lines added for the caching:

fastcgi\_cache MYCACHE;  
fastcgi\_keep\_conn on;  
fastcgi\_cache\_bypass $no\_cache $no\_cache\_dirs;  
fastcgi\_no\_cache $no\_cache $no\_cache\_dirs;  
fastcgi\_cache\_valid 200 301 5m;  
fastcgi\_cache\_valid 302 5m;  
fastcgi\_cache\_valid 404 1m;  
fastcgi\_cache\_use\_stale error timeout invalid\_header updating http\_500;  
fastcgi\_ignore\_headers Cache-Control Expires;  
expires epoch;  
fastcgi\_cache\_lock on;

Did a test on some of the no cache dirs (including a wordpress blog) and  
the pages in them were still getting cached. Any ideas why?

Thanks. Hope you all have great weekends.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 19, 2012, 1:43am UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/10 "2012-08-19T01:43:26Z")

</div>

On 18 Ago 2012 07h10 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> map $uri $no\_cache\_dirs {
> 
> fastcgi\_cache MYCACHE; fastcgi\_keep\_conn on; fastcgi\_cache\_bypass  
> $no\_cache $no\_cache\_dirs; fastcgi\_no\_cache $no\_cache $no\_cache\_dirs;  
> fastcgi\_cache\_valid 200 301 5m; fastcgi\_cache\_valid 302 5m;  
> fastcgi\_cache\_valid 404 1m; fastcgi\_cache\_use\_stale error timeout  
> invalid\_header updating http\_500; fastcgi\_ignore\_headers  
> Cache-Control Expires; expires epoch; fastcgi\_cache\_lock on;
> 
> Did a test on some of the no cache dirs (including a wordpress blog)  
> and the pages in them were still getting cached. Any ideas why?

Yes, probably you need a regex based map:

So use:

map $uri $no\_cache\_dirs {  
default 0;  
~^/(?:dir1|dir2|dir3|dir4|dir5) 1;  
}

instead.

— appa

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [August 1, 2012, 5:27pm UTC](https://www.ruby-forum.com/t/nginx-simple-caching-solutions/221825/11 "2012-08-01T17:27:56Z")

</div>

On 1 Ago 2012 17h18 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I set up the cache and restarted nginx. How do I debug it? I ab  
> tested the front page and it’s still slow with concurrent  
> requests. There seems to be a few snippets in the cache dir, but not  
> every php file is getting cached.

Use cURL. Add a header with the cache status:

## Add a cache miss/hit status header.

add\_header X-My-Cache $upstream\_cache\_status;

to your cache config. Check for this header when doing requests with  
cURL.

— appa
