もうすぐ社内で某大会が開催される。
それに触発された形にはなるけれど、そう言えば最近自宅サーバーが遅くて辛いなぁと思ってたので少しだけ手をいれることに。
と言ってもベタにリバースプロキシとして使ってるNginxでキャッシュさせるというもの。
アクセスが多いわけではないのにメモリが10GBくらいあるのでもったいないと思い、キャッシュディレクトリ用にはtmpfsを作成した。

[code]
$ sudo mkdir -p /mnt/nginx/{cache,tmp}
$ vim /etc/fstab
### 追記
nginxcache /mnt/nginx/cache tmpfs size=128M 0 0
nginxtmp /mnt/nginx/tmp tmpfs size=64M 0 0
$ sudo mount -a
$ df -h
nginxcache 128M 0K 128M 0% /mnt/nginx/cache
nginxtmp 64M 0 64M 0% /mnt/nginx/tmp
[/code]

んで上記tmpfsにproxyするようにNginxにサッと書いてreloadした。

[code]
http {
proxy_ignore_headers X-Accel-Redirect X-Accel-Expires Cache-Control Expires Set-Cookie;
proxy_cache_path /mnt/nginx/cache levels=1 keys_zone=cache-space:4m inactive=7d max_size=100m;
proxy_temp_path /mnt/nginx/tmp;
[/code]
[code]
location / {
proxy_cache cache-space;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 30m;
proxy_pass http://backend1;
}
[/code]

実際に速度を計測してみた。

変更前

[code]
% ab -c 1 -n 1 http://inamuu.com/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking inamuu.com (be patient)…..done

Server Software: nginx
Server Hostname: inamuu.com
Server Port: 80

Document Path: /
Document Length: 54478 bytes

Concurrency Level: 1
Time taken for tests: 5.186 seconds
Complete requests: 1
Failed requests: 0
Write errors: 0
Total transferred: 54934 bytes
HTML transferred: 54478 bytes
Requests per second: 0.19 [#/sec] (mean)
Time per request: 5186.223 [ms] (mean)
Time per request: 5186.223 [ms] (mean, across all concurrent requests)
Transfer rate: 10.34 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 5186 5186 0.0 5186 5186
Waiting: 5183 5183 0.0 5183 5183
Total: 5186 5186 0.0 5186 5186
[/code]

おっそワロタwwwってなるくらい遅い。

変更後

[code]
% ab -c 1 -n 1 http://inamuu.com/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking inamuu.com (be patient)…..done

Server Software: nginx
Server Hostname: inamuu.com
Server Port: 80

Document Path: /index.php
Document Length: 0 bytes

Concurrency Level: 1
Time taken for tests: 1.564 seconds
Complete requests: 1
Failed requests: 0
Write errors: 0
Non-2xx responses: 1
Total transferred: 415 bytes
HTML transferred: 0 bytes
Requests per second: 0.64 [#/sec] (mean)
Time per request: 1563.893 [ms] (mean)
Time per request: 1563.893 [ms] (mean, across all concurrent requests)
Transfer rate: 0.26 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 1564 1564 0.0 1564 1564
Waiting: 1561 1561 0.0 1561 1561
Total: 1564 1564 0.0 1564 1564
[/code]

大分マシになった。
と言ってもこれ何度かアクセスして、キャッシュさせたからだ。
メモリーは潤沢でもCPUが1コアでAMDの2.8GHzという、よくいって省電力仕様くんなので、CPUを使わせないで高速にする方法を色々模索していきたい。

カテゴリー: HomeServer