If you put this in /var/aegir/config/includes/fastcgi_params.conf:

fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=microcache:20m inactive=1d max_size=512M;

This can be added just before any instances of fastcgi_pass that you want cached:

fastcgi_cache microcache;
fastcgi_cache_key $server_name|$request_uri;
fastcgi_cache_valid 404 30m;
fastcgi_cache_valid any 5m;

The first block is saying that the nginx cache should be located at /tmp/nginx-cache, levels is just specifying the file layout, keys_zone=microcache is calling the cache "microcache", 20m is the amount of shared memory to consume. Inactive = 1d will purge inactive cache entries after 1 day, and max_size does just what you'd think it does: cache can't get bigger than that.

The second block uses the "microcache" zone, with the cache keys in a multi-site safe format. It will cache 404s for 30 minutes, and everything else for 5 minutes.

If you want it even faster, you can relocate the cache to /dev/shm on ubuntu, and it will be running out of a ramdisk. That way, you don't have to have memcache running just to cache your site output.

I think this should be shipped with the default Nginx configurations, as it can result in significant performance improvements for Nginx users.

Comments

omega8cc’s picture

Project: Hostmaster (Aegir) » Provision
Version: 6.x-1.x-dev » 6.x-2.x-dev

It is not that simple and it is not enough to add a few lines in fastcgi_params.conf.

We are using far more advanced version of Nginx native caching already in BOA but no, we can't add this by default here, as it requires also some integration in the global.inc level or you will end up with various weird issues, like using the same cache for logged in users etc.

Plus, since BOA-2.0.1 you no longer need to use "micro-caching" because we integrated Purge module.

Please look how it is done in Barracuda and Octopus:

http://drupal.org/project/barracuda
http://drupal.org/project/octopus
http://omega8.cc/speed-booster-cache-boost-and-advagg-108#speed

omega8cc’s picture

Title: Add optional caching options to Nginx config » Add microcaching to Nginx config by default
Assigned: Unassigned » omega8cc
Status: Active » Needs work

Nginx config for Aegir is already microcaching ready for a long time: http://drupalcode.org/project/provision.git/blob/refs/heads/6.x-1.x:/htt...

We just didn't introduce it in the default config included in every vhost: http://drupalcode.org/project/provision.git/blob/refs/heads/6.x-1.x:/htt... because we are using it (so far) only for more advanced setup in BOA, called 'Speed Booster'.

However, there is no reason to not include by default so called 'microcaching': http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n as a default DoS or simply high traffic/load guard.

We only need to make sure that it will cache requests only for anonymous visitors and that it will be valid for just one second, which is enough to protect every site and yet still serve live content, like it was without any caching.

I'm changing the title to reflect this concept more precisely.

omega8cc’s picture

Or better yet, we can simplify the way we use Nginx caching in BOA as @perusio suggests in his config: https://github.com/perusio/drupal-with-nginx

## Set a cache_uid variable for authenticated users.
map $http_cookie $cache_uid {
    default nil; # hommage to Lisp :)
    ~SESS[[:alnum:]]+=(?<session_id>[[:alnum:]]+) $session_id;
}

This method seems to be the best and is really simple to implement on the Nginx main config level directly.

[EDIT] In fact it is by @brianmercer and @perusio - http://forum.nginx.org/read.php?2,220510,220596#msg-220596

cweagans’s picture

I've been using 10 second microcaching in production with great success.

omega8cc’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.