FastCGIキャッシュの設定例

# vi /etc/nginx/conf.d/default.conf 
# levels=1:2
# キャッシュパスの配下のサブディレクトリの文字数を1階層目は1文字、2階層目は2文字で設定

# keys_zone=cache_fastcgi:30m
# キャッシュパスのゾーン名、ゾーンサイズを設定

# max_size=512M
# キャッシュのファイルサイズの最大値を設定

# inactive=600m
# TTL
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:30m max_size=512M inactive=600m;
fastcgi_ignore_headers "Vary";

server {
    listen 80;
    server_name 0.0.0.0;

    charset UTF-8;
    client_max_body_size 16M;
    root  /usr/share/nginx/html;
    index index.php index.html index.htm; 

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

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

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

    location ~* /\. {
        deny all;
    }

    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|swf|ico|pdf|svg|eot|ttf|woff)$ {
        access_log off;
    }

    location ~* /wp-login\.php|/wp-admin/((?!admin-ajax\.php).)*$ {
        satisfy any;
        allow 0.0.0.0/0;
        allow 127.0.0.1;
        deny all;
        auth_basic "basic authentication";
        auth_basic_user_file  "/home/kusanagi/.htpasswd";

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_buffers 8 256k;
            fastcgi_buffer_size 128k;
            fastcgi_intercept_errors on;
            fastcgi_read_timeout 120s;
        }
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        }
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 8 256k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 120s;

        set $do_not_cache 1; ## page cache
        set $device "pc";

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

        if ($query_string != "") {
            set $do_not_cache 1;
        }

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

        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 $do_not_cache 1;
        }

        if ($http_user_agent ~* " Android |\(iPad|Android; Tablet; .+Firefox") {
            set $device "tablet";
        }

        if ($http_user_agent ~* " Android .+ Mobile |\(iPhone|\(iPod|IEMobile|Android; Mobile; .+Firefox|Windows Phone") {
            set $device "smart";
        }

        fastcgi_cache        wpcache;
        fastcgi_cache_key    "$device:$request_method:$scheme://$host$request_uri";
        fastcgi_cache_valid  200 10m;
        fastcgi_no_cache     $do_not_cache;
        fastcgi_cache_bypass $do_not_cache;

        add_header X-F-Cache $upstream_cache_status;
        add_header X-Signature KUSANAGI;
    }
}