a-blog cmsをリバースプロキシ(nginx)で高速化

リバースプロキシ(英: Reverse proxy)または逆プロキシは、特定のサーバへの要求を必ず経由するように設置されたプロキシサーバ。一般的なプロキシとは異なり不特定多数のサーバを対象としない。リバースプロキシは、不特定多数のクライアントから寄せられる要求に対して、応答を肩代わりすることにより特定のサーバの負担を軽減したり、アクセスを制限することにより特定のサーバのセキュリティを高めたりする目的に用いられる。

Nginxは無料で利用できるオープンソースのハイパフォーマンスHTTPサーバ且つリバースプロキシで、IMAP/POP3のプロキシサーバとしても動作します。Igor Sysoevによって2002年に開発が始まり、2004年に最初のバージョンが公開されました。今では世界中のドメインのおよそ12.18% (22.2M)のWebサイトをNginxが稼働させています。 Nginxはその高いパフォーマンスと安定性、豊富な機能、設定の容易さ、消費リソースの低さで知られています。
// キャッシュファイルを指定 proxy_cache_path /var/cache/nginx/static_file_cache levels=1:2 keys_zone=cache_static_file:128m inactive=7d max_size=480m; proxy_temp_path /var/cache/nginx/temp; // apacheサーバを指定 upstream backend { // ここではapacheを8080で動作 server 127.0.0.1:8080; } server { listen 80; // nginxを80番ポートで起動 server_name atsu666.com; location / { // リバースプロキシの設定 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; set $mobile 0; // モバイルの変数を用意 // UserAgentをみてモバイルからのアクセスか判定 if ( $http_user_agent ~* (Android|iPhone) ) { set $mobile 1; // モバイルかの場合、変数に"1"をセット } // GETリクエスト以外はキャッシュをしない if ( $request_method != GET ) { proxy_pass http://backend; // すぐにapacheに転送 break; } // Cookieにsidがある場合、a-blog cmsにログインしているとみなしキャッシュしない if ( $http_cookie ~* sid ) { proxy_pass http://backend; // すぐにapacheに転送 break; } // キャッシュの設定 proxy_cache cache_static_file; // キャッシュキー、モバイルは別キャッシュに proxy_cache_key $scheme$host$uri$args$mobile; proxy_cache_valid 200 2h; proxy_cache_valid any 1m; proxy_pass http://backend; } }
#! /bin/sh sudo rm -rf /var/cache/nginx/static_file_cache/*
# visudo apache ALL=(ALL) NOPASSWD: /bin/sh /usr/bin/nginx_clear.sh
define('HOOK_ENABLE', 1);
/** * キャッシュのリフレッシュ時 * */ public function cacheRefresh() { exec("sudo /bin/sh /usr/bin/nginx_clear.sh"); } /** * キャッシュのクリア時 * */ public function cacheClear() { exec("sudo /bin/sh /usr/bin/nginx_clear.sh"); } /** * キャッシュの削除時 * */ public function cacheDelete() { exec("sudo /bin/sh /usr/bin/nginx_clear.sh"); }
// apacheのみ a-blog cmsキャッシュなし Concurrency Level: 5 Time taken for tests: 77.999 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 4223000 bytes HTML transferred: 4145000 bytes Requests per second: 6.41 [#/sec] (mean) Time per request: 779.990 [ms] (mean) Time per request: 155.998 [ms] (mean, across all concurrent requests) Transfer rate: 52.87 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 31 61 53.9 48 555 Processing: 414 714 93.7 710 1149 Waiting: 396 705 95.5 702 1145 Total: 460 775 111.7 763 1315 // apacheのみ a-blog cmsキャッシュあり Concurrency Level: 5 Time taken for tests: 37.678 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 3695000 bytes HTML transferred: 3561000 bytes Requests per second: 13.27 [#/sec] (mean) Time per request: 376.776 [ms] (mean) Time per request: 75.355 [ms] (mean, across all concurrent requests) Transfer rate: 95.77 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 54 347 488.9 219 5663 Processing: 0 27 25.3 21 148 Waiting: 0 19 23.5 12 148 Total: 134 374 484.7 246 5663 // nginx (プロキシサーバとして使用) Concurrency Level: 5 Time taken for tests: 19.248 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 3670000 bytes HTML transferred: 3533500 bytes Requests per second: 25.98 [#/sec] (mean) Time per request: 192.478 [ms] (mean) Time per request: 38.496 [ms] (mean, across all concurrent requests) Transfer rate: 186.20 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 39 100 111.7 87 1231 Processing: 37 92 43.5 86 510 Waiting: 35 77 35.2 72 406 Total: 76 192 119.6 174 1350