Help us understand the problem. What is going on with this article?

WordpressでNginx(FastCGIcache)の設定

ここまでの準備は
WordPress(PHP7+nginx) for Amazon Linux AMI 2016.09

nginx.conf

自分用の設定なので適宜変更してください。

nginx.conf
http {

    index   index.php index.html index.htm;

    #Fastcgi_cache
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=FCACHE:32m inactive=1d max_size=128m;
    fastcgi_cache_use_stale error timeout invalid_header http_500;

    server {
        listen       443 ssl http2;
        listen       [::]:443 ssl http2;
        server_name  localhost;
        root         /var/www/html;
        index        index.php;

        #include /etc/nginx/default.d/*.conf;

        #Fastcgi_cache
        set $do_not_cache 0;
        if ($request_method !~ ^(GET)$) {
                set $do_not_cache 1;
        }
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                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;
        }
        set $mobilef '';
        if ($http_user_agent ~* '(Mobile|Android|Kindle|BlackBerry|Opera Mini|Opera Mobi)') {
                set $mobilef 'mobile.';
        }
        fastcgi_cache_key "$mobilef$scheme://$host$request_uri";

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

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass  php-fpm;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            include fastcgi_params;

            fastcgi_cache_bypass $do_not_cache;
            fastcgi_no_cache $do_not_cache;
            fastcgi_cache FCACHE;
            fastcgi_cache_valid  200 5m;
            fastcgi_cache_valid  any 10m;
            fastcgi_pass_header X-Accel-Expires;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            add_header x-cache $upstream_cache_status;
        }

        location ~* ^.+.(jpg|jpeg|gif|png|css|js|flv|swf|ico|xml)$ {
            access_log  off;
            log_not_found off;
            expires 30d;
        }

        location ~ /(\.ht|\.user.ini|\.git|\.hg|\.bzr|\.svn) {
            deny  all;
        }

    }
}

include /etc/nginx/default.d/*.conf;
でインクルードされてる中にlocationがかぶると優先順位で効かない場合あるので注意。
注意というかコメントアウトでいいと思われ。
Amazon Linux AMI 2016.09以降は

fastcgi_pass  php-fpm;

モバイル判定のUAはWordpressの wp_is_mobile() とほぼ同等にした。

ChromeのNetworkでhttpヘッダーを確認して
x-cache: HIT
になってれば有効になっている

参考
- Nginxのfastcgi_cacheでWordPressを高速化
- [技術ブログvol.29] NginxのチューニングとFastCGIcacheの設
- Nginx の fastcgi cache を利用して WordPress を高速化する
- CentOSのNginxにFastCGIキャッシュを設定してサイト速度を100倍にする
- Nginxのキャッシュがうまく効いてなかったのを解決しました

Nginx Cache Controller

プラグインをインストールする。
Nginx Cache Controller

functions.php
function flush_cache($url) {
    $nginxchampuru = NginxChampuru::get_instance();

    global $wpdb;
    $table = $wpdb->prefix.'nginxchampuru';
    $sql = $wpdb->prepare(
        "select `cache_key`, `cache_id`, `cache_type`, ifnull(`cache_url`,\"\") as `cache_url` from `$table` where `cache_url` = %s",
        $url
    );
    $keys = $wpdb->get_results( $sql );
    $keys = array_map( function ( $key ) {
        return $key->cache_key;
    }, $keys );

    $caches = $nginxchampuru->get_cache_file( $keys );
    foreach ( $caches as $cache ) {
        if ( is_file( $cache ) ) {
            unlink( $cache );
        }
    }

    $sql = "delete from `$table` where cache_key in ('" . join( "','" , $keys ) . "')";
    $wpdb->query( $sql );
}
add_filter('nginxchampuru_flush_cache', 'flush_cache', 10, 1);

function get_key($url) {
    if ( wp_is_mobile() ) {
        return md5('mobile.' . $url);
    } else {
        return md5($url);
    }
}
add_filter('nginxchampuru_get_reverse_proxy_key', 'get_key', 10, 1);

あとはプラグインの管理画面で設定変更する。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした