Nginx simple caching solutions

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.

Have you tried the proxy_cache?
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache

2012/8/1 Ian M. Evans [email protected]:

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

Have you tried the proxy_cache?
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. :slight_smile:

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.

On 1 Ago 2012 15h02 CEST, [email protected] 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

On Wed, 1 Aug 2012 00:24:12 -0400, “Ian M. Evans”
[email protected] 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:

    fastcgi_cache_path /var/lib/nginx/fastcgicache levels=1:2
    keys_zone=MYCACHE:5m inactive=2h max_size=1g loader_files=1000
    loader_threshold=2000;

    map $http_cookie $no_cache { default 0; ~SESS 1; }

    fastcgi_cache_key “$scheme$request_method$host$request_uri”;

  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:

    fastcgi_cache MYCACHE;
    fastcgi_keep_conn on;
    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;

Done.

Regards,
M.

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.

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.

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.

On 18 Ago 2012 07h10 CEST, [email protected] 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

On 1 Ago 2012 17h18 CEST, [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs