WordPress
FastCGI
nginx

WordpressでNginx(FastCGIcache)の設定

More than 1 year has passed since last update.

ここまでの準備は

WordPress(PHP7+nginx) for Amazon Linux AMI 2016.09


nginx.conf

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


nginx.conf

http {

#Fastcgi_cache Settings
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:3m inactive=3d max_size=50m;
fastcgi_cache_use_stale error timeout invalid_header http_500;

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

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

client_max_body_size 4M;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:60m;

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

ssl_certificate_key /path/to/ssl_certificate_key;
ssl_certificate /path/to/ssl_certificate;

ssl_dhparam /etc/nginx/ssl/dhparam.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';

ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/ssl_certificate;
resolver 8.8.8.8;

add_header Strict-Transport-Security max-age=15768000;

#Fastcgi_cache Settings
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;
}

#For phpMyAdmin
location ^~ /phpmyadmin {
allow 0.0.0.0;#IP
deny all;

alias /var/www/phpmyadmin;
index index.php;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^/phpmyadmin(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /var/www/phpmyadmin$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
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 WORDPRESS;
fastcgi_cache_valid 200 5m;
fastcgi_cache_valid any 10m;
fastcgi_pass_header X-Accel-Expires;
}

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

}
}


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

でインクルードされてる中にlocationがかぶると優先順位で効かない場合あるので注意。

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

各設定の意味は下記ページを参考に。

参考

- Nginxのfastcgi_cacheでWordPressを高速化

- [技術ブログvol.29] NginxのチューニングとFastCGIcacheの設

- Nginx の fastcgi cache を利用して WordPress を高速化する


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);


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