nginx simple caching solutions

António P. P. Almeida appa at perusio.net
Wed Aug 1 13:13:16 UTC 2012


On 1 Ago 2012 15h02 CEST, ianevans at digitalhit.com wrote:

> On 01/08/2012 4:44 AM, Mark Alan wrote:
>>> 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;
>
> 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



More information about the nginx mailing list