Fastcgi_cache_path empty

I wanted to try fastcgi_cache on my nginx 1.5.8 as shown here
http://seravo.fi/2013/optimizing-web-server-performance-with-nginx-and-php

In nginx conf, http section, I added:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:10m
max_size=1000m inactive=60m;

In server section:
set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $cache_uri 'null cache';
}
if ($query_string != "") {
    set $cache_uri 'null cache';
}

# Don't cache uris containing the following segments
if ($request_uri ~*

“(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]±sitemap([0-9]+)?.xml)”)
{
set $cache_uri ‘null cache’;
}

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~*

“comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in”) {
set $cache_uri ‘null cache’;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;


    ##
    # Fastcgi cache
    ##
    set $skip_cache 1;
            if ($cache_uri != "null cache") {
                add_header X-Cache-Debug "$cache_uri $cookie_nocache

$arg_nocache$arg_comment $http_pragma $http_authorization";
set $skip_cache 0;
}
fastcgi_cache_bypass $skip_cache;
fastcgi_cache_key
$scheme$host$request_uri$request_method;
fastcgi_cache_valid any 8m;
fastcgi_cache_bypass $http_pragma;
fastcgi_cache_use_stale updating error timeout
invalid_header http_500;

}

I chowned /var/cache/nginx to www-data user (and group) and chmodded it to
775.
I restarted nginx but the folder is always empty. Is it normal? How can I
test if fastcgi_cache is working?

Thanks in advance

No one? :slight_smile:

2014/1/13 Lorenzo R. [email protected]

I think ( it’s hard to read ) that you’re not telling the site which
http cache to use ( fastcgi_cache microcache; ).

On my systems ( I’m looking at an Amazon one so RH
based ), /var/cache/nginx is already in use. I set
up /var/cache/nginx_fastcgi instead, which is not empty.

(1GB is way too big btw).

I created a file /etc/nginx/microcache, containing

–8<–

Setup var defaults

set $no_cache “”;

If non GET/HEAD, don’t cache & mark user as uncacheable for 1 second

via cookie
if ($request_method !~ ^(GET|HEAD)$) {
set $no_cache “1”;
}

Drop no cache cookie if need be

(for some reason, add_header fails if included in prior if-block)

if ($no_cache = “1”) {
add_header Set-Cookie “_mcnc=1; Max-Age=2; Path=/”;
add_header X-Microcachable “0”;
}

Bypass cache if no-cache cookie is set

if ($http_cookie ~* “_mcnc”) {
set $no_cache “1”;
}

Bypass cache if flag is set

fastcgi_no_cache $no_cache;
fastcgi_cache_bypass $no_cache;
fastcgi_cache microcache;
fastcgi_cache_key “$scheme$request_method$host$request_uri
$http_if_modified_since$http_if_none_match”;
fastcgi_cache_valid 404 30m;
fastcgi_cache_valid 200 10s;
fastcgi_max_temp_file_size 1M;
fastcgi_cache_use_stale updating;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
–8<–
(this only stores for 10 seconds BTW, and the cache key is designed for
a magento site).

And included it in the location blocks that would use it, ie

location ~* .php$

This way the config becomes much more readable.

hth,

Steve

On Thu, 2014-01-16 at 20:19 +0100, Lorenzo R. wrote:

            set $cache_uri 'null cache';
            set $cache_uri 'null cache';
            try_files $uri =404;
                        add_header X-Cache-Debug "$cache_uri
    invalid_header http_500;

nginx mailing list
[email protected]
nginx Info Page


Steve H. BSc(Hons) MIITP

Linkedin: http://www.linkedin.com/in/steveholdoway
Skype: sholdowa