bren サイトは、 nginx + WordPress で構成されています。
これを、
- nginx をリバースプロキシキャッシュとして使用
- 非キャッシュを Apache に処理させる
というよくある構成に変更し、速度を比較してみます。
詳細構成
Apache と WordPress プラグインを追加し、php-fpm を削除します。
変更前
- Nginx
- php-fpm
- php-apc
- WP Booster プラグイン(ベータテスト中)
変更後
- Nginx
- Apache
- php-fpm
- php-apc
- WordPress の WP Booster プラグイン(digital cube inc. 作ベータテスト中)
- WordPress の Nginx Cache Control プラグイン
計測結果
ab コマンドで、 10 クライアントから 100 回リクエストを送るという条件でテストしました。
変更前
# ab -c 10 -n 100 http://bren.jp/ Concurrency Level: 10 Time taken for tests: 15.755 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 1326500 bytes HTML transferred: 1301700 bytes Requests per second: 6.35 [#/sec] (mean) Time per request: 1575.520 [ms] (mean) Time per request: 157.552 [ms] (mean, across all concurrent requests) Transfer rate: 82.22 [Kbytes/sec] received
変更後
# ab -c 10 -n 100 http://bren.jp/ Concurrency Level: 10 Time taken for tests: 0.060 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 999800 bytes HTML transferred: 972700 bytes Requests per second: 1673.25 [#/sec] (mean) Time per request: 5.976 [ms] (mean) Time per request: 0.598 [ms] (mean, across all concurrent requests) Transfer rate: 16337.05 [Kbytes/sec] received
各種設定方法について
Apache 関連設定
次の apxs コマンドを使うために、httpd-devel をインストール
# yum install httpd-devel
Nginx to Apache 設定で使うので、 mod_extract_forwarded をインストール
# wget http://www.openinfo.co.uk/apache/extract_forwarded-2.0.2.tar.gz # tar zxf extract_forwarded-2.0.2.tar.gz # cd extract_forwarded # apxs -i -c -a mod_extract_forwarded.c
/etc/httpd/conf/httpd.conf を編集。
Nginx がポート 80 、Apache がポート 8080 を使うようにし、元々 Nginx 単体だったので、 Apache も ユーザーとグループを nginx にする。
…
Listen 8080 ... User nginx Group nginx ... ServerName bren.jp:8080
/etc/httpd/conf.d/vhost.conf を作成。
NameVirtualHost *:8080 # DocumentRoot <VirtualHost *:8080> ServerName example.com DocumentRoot /path/to # Nginx で使用していた公開パス ServerAdmin info@example.com ErrorLog /var/www/example.com/logs/error.log TransferLog /var/www/example.com/logs/access.log </VirtualHost>
Nginx 関連設定
/etc/nginx/conf.d/default.conf を編集。
# キャッシュ関連設定 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m; proxy_temp_path /var/tmp/nginx; proxy_cache_key "$scheme://$host$request_uri"; 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; # バックエンドサーバ-(Apache)設定 upstream backend { ip_hash; server 127.0.0.1:8080; } # # The default server # server { listen 80; server_name bren.jp; root /var/www/bren.jp/public; # /wp-admin 以下はバックエンドサーバ- location /wp-admin { proxy_pass http://backend; } # .php ファイルはバックエンドサーバ- location ~ .*\.php { proxy_pass http://backend; } # .ht(xxx) ファイルはアクセス禁止 location ~ /\.ht { deny all; } # 静的ファイルはアクセスログに記録せず、30日間キャッシュする location ~ .*\.(txt|xml|html?|js|css|gz|ico|jpe?g|gif|png|wmv|flv|swf|mpg) { access_log off; expires 30d; break; } # それ以外のファイルは # ・フィーチャーフォン/スマートフォンでキャシュ先を変更 # ・ログイン済みユーザーの場合はキャッシュOFF # としたうえで、バックエンドサーバ- location / { set $mobile ""; if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP\.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter)') { set $mobile "@ktai"; } if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry)') { set $mobile "@mobile"; } if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) { set $do_not_cache 1; } proxy_no_cache $do_not_cache; proxy_cache_bypass $do_not_cache; proxy_cache czone; proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile"; proxy_cache_valid 200 301 302 10m; proxy_cache_valid 404 5m; proxy_pass http://backend; } }
参考にさせていただいたサイト
Pingback: 7月23日の注目記事 | Javable.Jp
× proxy_cache_key “$scheme://$host$request_uri$is_args$args$mobile”;
○ proxy_cache_key “$scheme://$host$request_uri$mobile”;
こうじゃないと、http://example.com/?foo=bar?foo=bar みたいにクエリーが重複しちゃいます。
/var/cache/nginx内にキャッシュがテキストファイルで保存されてるので、それを開いてみるとキャッシュキーが確認できますよ。
ありがとうございます。 確認してみますー^^
Pingback: ド素人が完全自作SNSを作ってみてわかったこと。(3) - 増田まとめ
Pingback: さくらVPS 1GにNginxとPHP-FPMをいれてWordPressブログを作ったメモ | ninxit.blog
Pingback: ド素人が完全自作SNSを作ってみてわかったこと。(21) - 増田まとめ
Pingback: Apache+Nginxを検証してみた。 | スタヂオねこまねき