Nginx + fastcgi_cache stopped working

First time poster so go easy on me.

I followed your great guide on here last year:

And I had it working beautifully on a Ubuntu 12.04 LTS box.

I’ve since migrated to a new server running the latest Ubuntu 14.04, nginx 1.6.1, php5-fpm. Used for a wordpress 4.0 site.

Since the migration I’ve not been able to spot any file activity in /var/run. No cached files are created. Messed around with permissions quite generously but no fish.

W3TC and Wordfence plugins are currently disabled with only the nginx-helper plugin active.

Attached are the relevant nginx.conf and specific vhosts file sections. Really appreciate any guidance on this. I’m running ispconfig so the first chunks are standard ispconfig 3 default values.

nginx.conf:

    http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
	fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;
	fastcgi_cache_key "$scheme$request_method$host$request_uri";
	fastcgi_cache_use_stale error timeout invalid_header http_500;
	fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

	open_file_cache          max=10000 inactive=5m;
	open_file_cache_valid    2m;
	open_file_cache_min_uses 1;
	open_file_cache_errors   on;
	client_max_body_size 20M;

	.....
	}

vhosts file:

    server {
    listen *:80;


    server_name mysite.com www.mysite.com;

    root   /var/www/mysite.com/web;



    index index.html index.htm index.php index.cgi index.pl index.xhtml;



    error_page 400 /error/400.html;
    error_page 401 /error/401.html;
    error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 /error/500.html;
    error_page 502 /error/502.html;
    error_page 503 /error/503.html;
    recursive_error_pages on;
    location = /error/400.html {

        internal;
    }
    location = /error/401.html {

        internal;
    }
    location = /error/403.html {

        internal;
    }
    location = /error/404.html {

        internal;
    }
    location = /error/405.html {

        internal;
    }
    location = /error/500.html {

        internal;
    }
    location = /error/502.html {

        internal;
    }
    location = /error/503.html {

        internal;
    }

    error_log /var/log/ispconfig/httpd/mysite.com/error.log;
    access_log /var/log/ispconfig/httpd/mysite.com/access.log combined;

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico {
                    log_not_found off;
                    access_log off;
    }

    location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;
    }

    location /stats/ {
     index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file /var/www/clients/client2/web10/web/stats/.htpasswd_stats;
    }

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /354e8974e30823737ca1b7f51962fc1b.htm @php;
    }

    location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php5-fpm/web10.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }






    location = /xmlrpc.php {
                    deny all;
    }
    if ($http_user_agent ~* (Baiduspider|Jullo) ) {
                    return 444;
                 }

                 if ($http_referer ~* (babes|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|sex|teen|video|webcam|zippo) ) {
                    return 444;
                 }
   location ~ /\.ht {
                deny  all;
    }

    location ~* wp-admin {
                try_files $uri $uri/ =404;
                allow 173.33.194.111;
                allow 127.0.0.1;
                deny all;
    }

    location ~* wp-admin/includes {
    deny all;
    }
    location ~* wp-includes/theme-compat/ {
    deny all;
    }
    location ~* wp-includes/js/tinymce/langs/.*\.php {
    deny all;
    }
    location /wp-content/ {
    internal;
    }
    location /wp-includes/ {
    internal;
    }
    location ~* wp-config.php {
    deny all;
    }

    location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php|js|swf)$ {
                types { }
                default_type text/plain;
    }


            error_page 403 = 404;
   if ($request_method !~ ^(GET|POST|HEAD)$ ) {
    return 444;
    }


    server_tokens off;
    server_name_in_redirect off;


         set $skip_cache 0;

                if ($request_method = POST) {
                    set $skip_cache 1;
                }
                if ($query_string != "") {
                    set $skip_cache 1;
                }

                if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                    set $skip_cache 1;
                }

                if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
                    set $skip_cache 1;
                }
    location / {
                    try_files $uri $uri/ /index.php?$args;
    }


                            rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                            rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
                            rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
                            rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  location ~ .php$ {

                    fastcgi_cache_bypass $skip_cache;
                    fastcgi_no_cache $skip_cache;
                    fastcgi_cache WORDPRESS;
                    fastcgi_cache_valid  60m;
    }

    location ~ /purge(/.*) {
                    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }
            gzip on;
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|asf|asx|wax|wmv|wmx|avi|class|divx|docx|gzip|jpe|json|mdb|mov|qt|mp3|m4a|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|pdf|pot$
                    access_log off; log_not_found off;
                    expires 31536000s;
                add_header Pragma "public";
                add_header Cache-Control "max-age=31536000, public";
                add_header X-Powered-By "W3 Total Cache/0.9.4";
    }
                    gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
    location ~ \.(css|htc|less|js|js2|js3|js4)$ {
                        expires max;
                        add_header Pragma "public";
                        add_header Cache-Control "max-age=604800, public";
    }
    location ~ \.(html|htm|rtf|rtx|txt|xsd|xsl)$ {
                        expires 3200s;
                        add_header Pragma "public";
                        add_header Cache-Control "max-age=3200, public";
                        add_header X-Powered-By "W3 Total Cache/0.9.4";
    }

}

Any guidance greatly appreciated.

No takers? :smile:

@ichpen

Check if your nginx has fastcgi_cache_purge module

Refer: https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/#prerequisites

Also send us the output of following command

nginx -V 2>&1 | grep nginx-cache-purge -o

It does have cache purge and here’s the output. nginx-cache-purge

So I’ve tried a few combinations including moving the location / directives to the bottom, commenting out skip_cache etc. Still nothing.

Definitely something stupid that may be missing but running out of ideas.

Am I boring everyone? :smile: Lol

Guess I’ll keep on with my monologue.

After some digging I’m finding that adding a header (either in http or server block) with $upstream_cache_status actually doesn’t return any value. No header is added at all. Unfortunately nginx isn’t compiled with debug so there’s very little debug info coming out in the error logs.

Using any other variable like $upstream_addr works just fine and I can see the header being added. So there’s something very fishy going on. I suspect very relevant to my problem.