nginx

Nginx fast cgi cache

Nginxでfast cgi cache をwordpress で使用するには、

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

効果

簡単な効果判定だが、

apach bench  ab で速度計測を行った。

考えるのが面倒なので、

ab のパラメーター
-n 100 : 総リクエスト数
-c 100 : 同時リクエスト数

同時に100リクエストされた場合とした。

				
					root@wordpress:~# ab -n 100 -c 100 https://example.com/
This is ApacheBench, Version 2.3 <$Revision: 1913912 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking example.com (be patient).....done


Server Software:        nginx/1.22.1
Server Hostname:        example.com
Server Port:            443
SSL/TLS Protocol:       TLSv1.3,TLS_AES_256_GCM_SHA384,256,256
Server Temp Key:        X25519 253 bits
TLS Server Name:        example.com

Document Path:          /
Document Length:        52917 bytes

Concurrency Level:      100
Time taken for tests:   0.067 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      5326800 bytes
HTML transferred:       5291700 bytes
Requests per second:    1485.55 [#/sec] (mean)
Time per request:       67.315 [ms] (mean)
Time per request:       0.673 [ms] (mean, across all concurrent requests)
Transfer rate:          77277.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2   32  11.9     31      54
Processing:     7   29  12.2     30      60
Waiting:        0   16   9.6     16      33
Total:         60   61   0.4     61      62

Percentage of the requests served within a certain time (ms)
  50%     61
  66%     61
  75%     61
  80%     61
  90%     61
  95%     61
  98%     61
  99%     62
 100%     62 (longest request)
				
			

結果として、めちゃくちゃ高速になる。
まじで、Nginx のキャッシュは高速になる。

ちょっと Apache|2   nginx | nginx+fast_cgi_cache と比べてみよう。

ab -n 100 -c 100 https://example.com/

				
					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/1.22.1
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 で測定した最後の項目に注目すると、

キャッシュなしの場合、

計測の結果、Apache/2.4.62 は nginx/1.22.1 よりも高速に動作した。(ちょっと驚き!)

Apache/2.4.62は、数年前に測定した結果よりも早くなった(php-fpm を使ったから)

しかし、圧巻なのが、nginx/1.22.1 Fast cgi cacheを使うと、10倍くらい高速に動作し、たくさんの同時アクセスをこなすことができるようになります。

ただし、キャッシュは、更新を行ってもすぐには変更されないことが問題となりますので、十分に検証してから使用した方が良いでしょう。

WordPress plugin Nginx Cache

プラグイン 「Nginx Cache」を使います。

コンテンツが変更されたときに Nginx キャッシュ (FastCGI、プロキシ、uWSGI) を自動的に削除するプラグインです。管理画面内で手動で削除することもできます。

nginx cache plugin

普通のプラグインと同様にインストールを行い、キャッシュのパスを入力します。

http{
fastcgi_cache_path /var/cache/nginx/fastcgi …….

とNginxで設定したキャッシュのパスを入力して変更を保存すれば良いだけです。

Related Posts

wordpress icon

WP-CLI インストール

WP-CLI は WordPress を管理するためのコマンドラインインターフェースで、プラグインのアップデートなどの実行ができる。