Nginx – Working Nginx fastcgi_cache php-fpm cache and static file caching configuration

cachemariadbnginxphp-fpm

Nginx wiki is very vague with explanation as to how to properly setup nginx with php-fpm with fastcgi_caching for sites which have cookies i.e. wordpress, drupal, vbulletin etc.

I downloaded a modified nginx bash shell install script called centmin from http://vbtechsupport.com/796/ and while it installs nginx v1.0.2, php 5.3.6 php-fpm, mariadb 5.2.6 mysql, memcached 1.4.5 servers and siege benchmark automatically via shell script, it lacks the configuration parameters for setting up fastcgi_caching for caching php.

It also lacks setup for caching static files for locally served files. Is there any point in using proxy_cache for static files when they reside on the same disk ?

Anyone got some tips and info links to info to read up on tutorials for proper setup for php (php-fpm) fastcgi_caching as well as for caching locally residing static files ?

thanks

Best Answer

Is there any point in using proxy_cache for static files when they reside on the same disk ?

Nope, none. It's disk access to static files either way.

Anyone got some tips and info links to info to read up on tutorials for proper setup for php (php-fpm) fastcgi_caching as well as for caching locally residing static files ?

Take a look at the various proxy_caching tutorials out there, especially those that proxy WordPress from Apache -- the fastcgi_caching is very nearly identical, and what works for proxy_caching will almost undoubtedly work just as well for fastcgi_caching.

I actually happen to be working on this exact same problem myself right now. So far I've got it working except for accounting for the cookies, but that's just a simple series of if directives to set up additional variables for the fastcgi_cache_key directive. This page should prove very useful for you; just skip down to the proxy_caching configuration and change all those proxy_* directives to fastcgi_* (this is what I've been following, but beware that if is evil and should not reside within a location directive...).

When I have my own worked out fully I'll be posting it on my blog (link is in my profile; I'll get in trouble again if I put that link in my post). It's really a shame there aren't, well, any fastcgi_caching guides out there already, so I can't point you to anything except my blog (even though it's not there quite yet...).

Edited to add: Here's my current fastcgi_caching configuration. Like I said, it yet lacks any accounting for cookies, but it is indeed actually functional as-is, for the most part.

Within the location block that processes my .php files, I've added:

#Caching parameters
fastcgi_cache one;
#I use host here to account for the fact that I have multiple WP instances
fastcgi_cache_key $scheme$host$request_uri;

fastcgi_cache_valid  200 302 304 30m;
fastcgi_cache_valid  301 1h;
fastcgi_cache_valid  any 5m;
fastcgi_cache_use_stale error timeout invalid_header updating http_500;

This ties into the additions I've made within the http block:

# configure cache log
log_format cache '$remote_addr - $host [$time_local]  '
                 '"$request" $status $upstream_cache_status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';


# Configure cache and temp paths
fastcgi_cache_path /var/cache/nginx levels=1:2
                   keys_zone=one:100m
                   inactive=7d max_size=10g;
fastcgi_temp_path /var/cache/nginx/tmp;