Fast cgi cache の設定
Nginxのキャッシュの一つ、Fast cgi cache を設定してみる
※ このキャッシュの設定は、最初の設定段階で行うと、トラブルの時に原因がわからなくなるので、全ての修正が終わってからキャッシュの設定を改めて行うことが良いと考えます。
Nginx設定ファイル
設定ファイルの http{ } と server{ } にそれぞれ設定を記述する必要がある。
設定のやり方が色々あるが、
http ディレクティブ
http{
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
}
serverディレクティブ
server{
......
location ~ \.php$ {
......
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_lock on;
}
}
キャッシュするディレクトを作成する。
/var/cache/nginx/fastcgi
# cd /var/cache
/var/cache# mkdir -p nginx/fastcgi
nginx再起動で設定を反映
設定が間違っていないか
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
設定の反映
# systemctl restart nginx.service
結果
Fastcig キャッシュの有無
簡単な効果判定だが、
apach bench ab で速度計測を行った。
考えるのが面倒なので、
ab のパラメーター
-n 100 : 総リクエスト数
-c 100 : 同時リクエスト数
同時に100リクエストされた場合とした。
% ab -n 100 -c 100 https://nginx.home/
Document Length: 80459 bytes
Concurrency Level: 100
Time taken for tests: 1.973 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 8069600 bytes
HTML transferred: 8045900 bytes
Requests per second: 50.69 [#/sec] (mean)
Time per request: 1972.650 [ms] (mean)
Time per request: 19.727 [ms] (mean, across all concurrent requests)
Transfer rate: 3994.86 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 43 107 15.8 98 128
Processing: 57 902 509.5 927 1759
Waiting: 38 898 511.3 924 1758
Total: 155 1009 522.0 1023 1885
Percentage of the requests served within a certain time (ms)
50% 1023
66% 1309
75% 1457
80% 1578
90% 1724
95% 1842
98% 1862
99% 1885
100% 1885 (longest request)
% ab -n 100 -c 100 https://nginx.home/
Document Length: 80459 bytes
Concurrency Level: 100
Time taken for tests: 0.243 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 8071801 bytes
HTML transferred: 8045900 bytes
Requests per second: 411.08 [#/sec] (mean)
Time per request: 243.259 [ms] (mean)
Time per request: 2.433 [ms] (mean, across all concurrent requests)
Transfer rate: 32404.22 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 38 108 20.4 93 132
Processing: 21 47 21.8 60 183
Waiting: 4 29 13.7 40 41
Total: 150 154 7.5 153 221
Percentage of the requests served within a certain time (ms)
50% 153
66% 154
75% 155
80% 158
90% 159
95% 160
98% 161
99% 221
100% 221 (longest request)
結果として、このサイトでは8倍高速になった。
まじで、Nginx のキャッシュは高速になる。
Apache vs Nginx vs Nginx-fastcgi-cache
ちょっと Apache2 nginx nginx+fast_cgi_cache と比べてみよう。
Apache/2.4.62
time (ms)
50% 267
66% 346
75% 398
80% 408
90% 458
95% 487
98% 493
99% 494
100% 494
nginx/1.22.1
time (ms)
50% 426
66% 534
75% 599
80% 635
90% 710
95% 742
98% 764
99% 771
100% 771
nginx Fast cgi cache
time (ms)
50% 40
66% 43
75% 44
80% 44
90% 46
95% 46
98% 46
99% 47
100% 47
apache bench で測定した最後の項目に注目すると、
- キャッシュなしのApache2とNginx場合、
計測の結果、Apache/2.4.62 は nginx/1.22.1 よりも高速に動作した。(ちょっと驚き!)
Apache/2.4.62は、数年前に測定した結果よりも早くなった(php-fpm のおかげか) - nginx1.22.1 の Fast cgi cache の有無
10倍近く高速に動作し、たくさんの同時アクセスを短時間で処理できます。
WordPress plugin Nginx Cache
キャッシュの欠点は、元のウエブページを変更しても変更内容がすぐには反映できない。(キャッシュを維持する時間が設定されているので、)
WordPressの場合、プラグイン 「Nginx Cache」を使います。
コンテンツが変更されたときに Nginx キャッシュ (FastCGI、プロキシ、uWSGI) を自動的に削除するプラグインです。管理画面内で手動で削除することもできます。

普通のプラグインと同様にインストールを行い、キャッシュのパス(nginx.confで設定した)を入力します。
http{
fastcgi_cache_path /var/cache/nginx/fastcgi …….
とNginxで設定したキャッシュのパスを入力して変更を保存すれば良いだけです。
Lighthouseパフォーマンステスト
google chrome WEBブラウザにあるデベロッパーツールの中のLighthouse のパフォーマンステストを行ってみた。
NginxのキャッシュONにしても、Lighthouse のパフォーマンスの向上は全くなかった。
キャッシュの影響は少ないのか。

パフォーマンステストの考察
サーバーの環境
Proxmox LXC
CPU usage 0.00% of 2 CPU(s)
Memory usage 6.96% (142.54 MiB of 2.00 GiB)
SWAP usage 10.42% (53.38 MiB of 512.00 MiB)
Bootdisk size 69.10% (1.31 GiB of 1.90 GiB)
のような環境下でのテストです。
ただ、ハードディスクからSSD に変わってきて、単ページの表示速度は、キャッシュからの表示速度と遜色なくなったと考えれば、納得のいく結果である。
ただし、複数の同時アクセスの場合は、キャッシュの効果が大きく現れるのだろう。
過去のサーバーは、ハードディスクのアクセススピードが足枷になっていたと考えれば Lighthouse でもキャッシュの効果はあったのだが、SSDのアクセススピードがキャッシュと遜色のない結果をもたらしたのだろうか?