Nginx not caching wordpress site

cachenginxWordpress

I have a website set up to use nginx caching. Here are the details:

    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    set $no_cache 0;

    if ($request_method = POST)
    {
        set $no_cache 1;
    }

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

    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $no_cache 1;
    }

    if ($http_cookie = "PHPSESSID")
    {
        set $no_cache 1;
    }


    root /var/www/websites/headstuff.org;
    index index.php;
    server_name _; 


    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    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)$ 
    {
        access_log off;
        log_not_found off;
        expires 365d; #max;
        add_header Cache-Control "public";
    }


    location ~ [^/]\.php(/|$) 
    {
        try_files $uri $uri/ /index.php?q=$uri&$args;

        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply
        add_header X-Fastcgi-Cache $upstream_cache_status;
        fastcgi_cache_bypass $no_cache;
        fastcgi_no_cache $no_cache;


        # Regular PHP-FPM stuff:
        include fastcgi_params;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_read_timeout 180;

        if (!-f $document_root$fastcgi_script_name) {
           return 404;
        }
   }
}

But when I try

curl -X GET -I headstuff.org

I get the following

HTTP/1.1 301 Moved Permanently
Date: Fri, 05 Feb 2016 10:48:22 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d22f1949b207c61fa8f304a6be8d01e4a1454669298; expires=Sat, 04-Feb-17 10:48:18 GMT; path=/; domain=.headstuff.org; HttpOnly
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Location: http://www.headstuff.org/
Pragma: no-cache
Set-Cookie: PHPSESSID=03eubavpjg3ct5tsiu1g2eqi63; path=/
Vary: Cookie
X-Fastcgi-Cache: MISS
X-Pingback: http://www.headstuff.org/xmlrpc.php
Server: cloudflare-nginx
CF-RAY: 26fe0acb87752999-DUB

Which always says X-Fastcgi-Cache: MISS

Any ideas what's happening here?

Best Answer

First, you configured nginx to not cache 301 redirect responses. If you want to cache these, (and you should!) then set it up:

fastcgi_cache_valid 301 30d;

Second, WordPress sent explicit instructions to not cache the redirect, so it would not be cached anyway.