wp-cron の呼び出しをコントロールする

アクセス数が多いサイトだと、wp-cron.php へのアクセスが逆にネックになる事もあります。
参考:DISABLE_WP_CRONをしてもNginx Cache Controllerプラグインがwp-cron.phpにリクエストし続ける件 – エロサイトの作り方

そんなとき、僕は Nginx で wp-cron.php へのリクエストを完全に遮断して

location = /wp-cron.php {
    return 403;
}

サーバで、Nginx の実行ユーザで cron を設定

$ sudo crontab -u nginx -e
*/5 * * * * /usr/bin/php /path/to/wordpress/wp-cron.php

って感じすかね。

[Nginx][WordPress] WordPress リバースプロキシキャッシュ用の nginx.conf

/etc/nginx/php-fpm

try_files $uri =404;

expires        off;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass   unix:/var/run/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_busy_buffers_size 32k;
fastcgi_temp_file_write_size 256k;

/etc/nginx/conf.d/default.conf

upstream backend {
  server unix:/var/run/nginx-backend.sock;
}

# reverse proxy
server {
    listen      80 default;
    server_name _;
    root        /path/to/wordpress;
    index       index.html index.htm;
    charset     utf-8;

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log;

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

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

    set $do_not_cache 0;
    if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
        set $do_not_cache 1;
    }
    if ($request_method = POST) {
        set $do_not_cache 1;
    }

    location / {
        try_files $uri @wordpress;

        # Pass all .php files onto a php-fpm/php-fcgi server.
        location ~ \.php$ {
            include /etc/nginx/php-fpm;
        }
    }

    location @wordpress {
        proxy_no_cache     $do_not_cache;
        proxy_cache_bypass $do_not_cache;

        proxy_redirect     off;
        proxy_cache        czone;
        proxy_cache_key    "$scheme://$host$request_uri";
        proxy_cache_valid  200 10m;
        proxy_cache_valid  404 5m;
        proxy_pass     http://backend;
    }

    # 404
    error_page 404 @not_found;
    location @not_found {
        internal;
        rewrite ^ /404;
    }
}

# backend
server {
    listen      unix:/var/run/nginx-backend.sock default;
    server_name _;
    root        /path/to/wordpress;
    index       index.php index.html index.htm;

    access_log  /var/log/nginx/backend.access.log main;

    keepalive_timeout 25;
    port_in_redirect  off;

    gzip              off;
    gzip_vary         off;

    # This order might seem weird - this is attempted to match last if rules below fail.
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ \.php$ {
        set $proxy_https '';
        if ( $http_x_forwarded_proto = 'https' ) {
            set $proxy_https 'on';
        }

        include /etc/nginx/php-fpm;
        fastcgi_param  REMOTE_ADDR      $http_x_real_ip;
        fastcgi_param  HTTPS            $proxy_https  if_not_empty;
        fastcgi_pass_header "X-Accel-Redirect";
        fastcgi_pass_header "X-Accel-Buffering";
        fastcgi_pass_header "X-Accel-Charset";
        fastcgi_pass_header "X-Accel-Expires";
        fastcgi_pass_header "X-Accel-Limit-Rate";
    }
}