blog/content/post/2011-02-02-00001454.md

296 lines
17 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: さくら VPS の Web サーバ変更を検討中
author: kazu634
date: 2011-02-02
url: /2011/02/02/_1658/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5413;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- つれづれ
---
<div class="section">
<p>
さくら VPS の Web サーバを Apache2 から nginx へ変更することを検討しています。特に理由はないのですが、強いて言えば
</p>
<ul>
<li>
レスポンスの向上
</li>
<li>
軽量な Web サーバを試してみたかった!
</li>
</ul>
<p>
といったところです。
</p>
<h4>
軽量な Web サーバを調べてみた
</h4>
<p>
Apache以外の軽量なWebサーバとして有名なものとして、以下の二つを発見しました:
</p>
<ul>
<li>
lighttpd
</li>
<li>
nginx
</li>
</ul>
<h4>
lighttpd
</h4>
<p>
軽量な Web サーバとしてはじめに検討したのが、lighttpd でした。これはネーミングからして簡単に推測できます。Light な httpd です。こいつは Web サーバのみの機能を持ち、軽量なようです。しかし、次の記事を読んで nginx に心移りしてしまったのです。
</p>
<ul>
<li>
<a href="http://kray.jp/blog/wordpress-tuning/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://kray.jp/blog/wordpress-tuning/', 'WordPressを100倍速くする MySQLの調整やnginx proxy cache | KRAY Inc');" target="_blank">WordPressを100倍速くする MySQLの調整やnginx proxy cache | KRAY Inc</a>
</li>
</ul>
<h4>
nginx
</h4>
<p>
nginx は Engine X (エンジン・エックス) と発音します。軽量な Web サーバ かつ、リバースプロキシサーバです。プロキシサーバも兼ねる、という点が私の心にヒットしました。普通はこのような構成になるはずなんです:
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110202223001" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110202223001', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110202/20110202223001.png" alt="f:id:sirocco634:20110202223001p:image" title="f:id:sirocco634:20110202223001p:image" class="hatena-fotolife" /></a>
</p>
<p>
それが nginx を導入するだけで、
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110202223000" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110202223000', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110202/20110202223000.png" alt="f:id:sirocco634:20110202223000p:image" title="f:id:sirocco634:20110202223000p:image" class="hatena-fotolife" /></a>
</p>
<p>
という構成が簡単に作れるのです。nginxを覚えれば、一度で Web サーバとリバースプロキシーを作れちゃうのがお得♪という感じなわけです。
</p>
<h4>
まとめ
</h4>
<p>
とりあえず nginx で頑張ってみる。ローカルの環境だと、なぜかリバースプロキシーをかませるとレスポンス悪くなるんだけど、これはなぜなのだろう。。。もう少し検証してみることにしよう。
</p>
<h4>
追記
</h4>
<p>
nginx.conf
</p>
<pre class="syntax-highlight">
<span class="synComment"># You may add here your</span>
<span class="synComment"># server {</span>
<span class="synComment"># ...</span>
<span class="synComment"># }</span>
<span class="synComment"># statements for each of your virtual hosts</span>
server {
listen 8001;
server_name srv634;
access_log /var/log/nginx/nginx.access.log;
location / {
root /var/www/nginx-default;
index index.html index.htm index.php;
}
location /wordpress {
root /var/www/nginx-default;
index index.php;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /wordpress/index.php last;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
}
<span class="synComment">#error_page 404 /404.html;</span>
<span class="synComment"># redirect server error pages to the static page /50x.html</span>
<span class="synComment">#</span>
<span class="synComment">#error_page 500 502 503 504 /50x.html;</span>
<span class="synComment">#location = /50x.html {</span>
<span class="synComment"># root /var/www/nginx-default;</span>
<span class="synComment">#}</span>
<span class="synComment"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
<span class="synComment">#</span>
<span class="synComment">#location ~ \.php$ {</span>
<span class="synComment">#proxy_pass http://127.0.0.1;</span>
<span class="synComment">#}</span>
<span class="synComment"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
<span class="synComment">#</span>
<span class="synComment">#location ~ \.php$ {</span>
<span class="synComment">#fastcgi_pass 127.0.0.1:9000;</span>
<span class="synComment">#fastcgi_index index.php;</span>
<span class="synComment">#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;</span>
<span class="synComment">#includefastcgi_params;</span>
<span class="synComment">#}</span>
<span class="synComment"># deny access to .htaccess files, if Apache's document root</span>
<span class="synComment"># concurs with nginx's one</span>
<span class="synComment">#</span>
<span class="synComment">#location ~ /\.ht {</span>
<span class="synComment">#deny all;</span>
<span class="synComment">#}</span>
}
upstream backend {
ip_hash;
server 127.0.0.1:8001;
}
server {
listen 80;
server_name srv634;
access_log /var/log/nginx/front_proxy.access.log;
error_log /var/log/nginx/front_proxy.error.log;
location /wp-admin { proxy_pass http://backend; }
location /wp-login.php { proxy_pass http://backend; }
location / {
proxy_pass http://backend;
proxy_cache czone;
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
proxy_cache_valid 200 10m;
}
}
</pre>
<p>
abの結果(リバースプロキシーをかませない場合)
</p>
<pre class="syntax-highlight">
kazu634@kazu634% ab <span class="synSpecial">-n</span> <span class="synConstant">5000</span> <span class="synSpecial">-c</span> <span class="synConstant">100</span> http://srv634:<span class="synConstant">8001</span>/wordpress ~ <span class="synStatement">[</span><span class="synConstant">1682</span><span class="synStatement">]</span>
This is ApacheBench, Version <span class="synConstant">2</span>.<span class="synConstant">3</span> <span class="synStatement">&#60;</span><span class="synPreProc">$Revision</span>: <span class="synConstant">655654</span> $<span class="synStatement">&#62;</span>
Copyright <span class="synConstant">1996</span> Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking srv634 <span class="synStatement">(</span>be patient<span class="synStatement">)</span>
Completed <span class="synConstant">500</span> requests
Completed <span class="synConstant">1000</span> requests
Completed <span class="synConstant">1500</span> requests
Completed <span class="synConstant">2000</span> requests
Completed <span class="synConstant">2500</span> requests
Completed <span class="synConstant">3000</span> requests
Completed <span class="synConstant">3500</span> requests
Completed <span class="synConstant">4000</span> requests
Completed <span class="synConstant">4500</span> requests
Completed <span class="synConstant">5000</span> requests
Finished <span class="synConstant">5000</span> requests
Server Software: nginx/<span class="synConstant"></span>.<span class="synConstant">7</span>.<span class="synConstant">65</span>
Server Hostname: srv634
Server Port: <span class="synConstant">8001</span>
Document Path: /wordpress
Document Length: <span class="synConstant">185</span> bytes
Concurrency Level: <span class="synConstant">100</span>
Time taken <span class="synStatement">for</span> tests: <span class="synConstant">1</span>.<span class="synConstant">890</span> seconds
Complete requests: <span class="synConstant">5000</span>
Failed requests: <span class="synConstant"></span>
Write errors: <span class="synConstant"></span>
Non-2xx responses: <span class="synConstant">5000</span>
Total transferred: <span class="synConstant">1920000</span> bytes
HTML transferred: <span class="synConstant">925000</span> bytes
Requests per second: <span class="synConstant">2645</span>.<span class="synConstant">01</span> <span class="synComment">[#/sec] (mean)</span>
Time per request: <span class="synConstant">37</span>.<span class="synConstant">807</span> <span class="synStatement">[</span>ms<span class="synStatement">]</span> <span class="synStatement">(</span>mean<span class="synStatement">)</span>
Time per request: <span class="synConstant"></span>.<span class="synConstant">378</span> <span class="synStatement">[</span>ms<span class="synStatement">]</span> <span class="synStatement">(</span>mean, across all concurrent requests<span class="synStatement">)</span>
Transfer rate: <span class="synConstant">991</span>.<span class="synConstant">88</span> <span class="synStatement">[</span>Kbytes/sec<span class="synStatement">]</span> received
Connection Times <span class="synStatement">(</span>ms<span class="synStatement">)</span>
min mean<span class="synStatement">[</span>+/-sd<span class="synStatement">]</span> median max
Connect: <span class="synConstant"></span> <span class="synConstant">1</span> <span class="synConstant"></span>.<span class="synConstant">4</span> <span class="synConstant">1</span> <span class="synConstant">4</span>
Processing: <span class="synConstant">6</span> <span class="synConstant">36</span> <span class="synConstant">2</span>.<span class="synConstant">6</span> <span class="synConstant">37</span> <span class="synConstant">41</span>
Waiting: <span class="synConstant">6</span> <span class="synConstant">36</span> <span class="synConstant">2</span>.<span class="synConstant">6</span> <span class="synConstant">37</span> <span class="synConstant">41</span>
Total: <span class="synConstant">9</span> <span class="synConstant">37</span> <span class="synConstant">2</span>.<span class="synConstant">4</span> <span class="synConstant">38</span> <span class="synConstant">43</span>
Percentage of the requests served within a certain <span class="synStatement">time</span> <span class="synStatement">(</span>ms<span class="synStatement">)</span>
<span class="synConstant">50</span>% <span class="synConstant">38</span>
<span class="synConstant">66</span>% <span class="synConstant">38</span>
<span class="synConstant">75</span>% <span class="synConstant">38</span>
<span class="synConstant">80</span>% <span class="synConstant">38</span>
<span class="synConstant">90</span>% <span class="synConstant">39</span>
<span class="synConstant">95</span>% <span class="synConstant">39</span>
<span class="synConstant">98</span>% <span class="synConstant">39</span>
<span class="synConstant">99</span>% <span class="synConstant">39</span>
<span class="synConstant">100</span>% <span class="synConstant">43</span> <span class="synStatement">(</span>longest request<span class="synStatement">)</span>
</pre>
<p>
abの結果(プロキシサーバをかませた場合):
</p>
<pre class="syntax-highlight">
kazu634@kazu634% ab <span class="synSpecial">-n</span> <span class="synConstant">5000</span> <span class="synSpecial">-c</span> <span class="synConstant">100</span> http://srv634/wordpress ~ <span class="synStatement">[</span><span class="synConstant">1683</span><span class="synStatement">]</span>
This is ApacheBench, Version <span class="synConstant">2</span>.<span class="synConstant">3</span> <span class="synStatement">&#60;</span><span class="synPreProc">$Revision</span>: <span class="synConstant">655654</span> $<span class="synStatement">&#62;</span>
Copyright <span class="synConstant">1996</span> Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking srv634 <span class="synStatement">(</span>be patient<span class="synStatement">)</span>
Completed <span class="synConstant">500</span> requests
Completed <span class="synConstant">1000</span> requests
Completed <span class="synConstant">1500</span> requests
Completed <span class="synConstant">2000</span> requests
Completed <span class="synConstant">2500</span> requests
Completed <span class="synConstant">3000</span> requests
Completed <span class="synConstant">3500</span> requests
Completed <span class="synConstant">4000</span> requests
Completed <span class="synConstant">4500</span> requests
Completed <span class="synConstant">5000</span> requests
Finished <span class="synConstant">5000</span> requests
Server Software: nginx/<span class="synConstant"></span>.<span class="synConstant">7</span>.<span class="synConstant">65</span>
Server Hostname: srv634
Server Port: <span class="synConstant">80</span>
Document Path: /wordpress
Document Length: <span class="synConstant">185</span> bytes
Concurrency Level: <span class="synConstant">100</span>
Time taken <span class="synStatement">for</span> tests: <span class="synConstant">5</span>.<span class="synConstant">583</span> seconds
Complete requests: <span class="synConstant">5000</span>
Failed requests: <span class="synConstant"></span>
Write errors: <span class="synConstant"></span>
Non-2xx responses: <span class="synConstant">5000</span>
Total transferred: <span class="synConstant">1920000</span> bytes
HTML transferred: <span class="synConstant">925000</span> bytes
Requests per second: <span class="synConstant">895</span>.<span class="synConstant">52</span> <span class="synComment">[#/sec] (mean)</span>
Time per request: <span class="synConstant">111</span>.<span class="synConstant">666</span> <span class="synStatement">[</span>ms<span class="synStatement">]</span> <span class="synStatement">(</span>mean<span class="synStatement">)</span>
Time per request: <span class="synConstant">1</span>.<span class="synConstant">117</span> <span class="synStatement">[</span>ms<span class="synStatement">]</span> <span class="synStatement">(</span>mean, across all concurrent requests<span class="synStatement">)</span>
Transfer rate: <span class="synConstant">335</span>.<span class="synConstant">82</span> <span class="synStatement">[</span>Kbytes/sec<span class="synStatement">]</span> received
Connection Times <span class="synStatement">(</span>ms<span class="synStatement">)</span>
min mean<span class="synStatement">[</span>+/-sd<span class="synStatement">]</span> median max
Connect: <span class="synConstant"></span> <span class="synConstant">1</span> <span class="synConstant"></span>.<span class="synConstant">6</span> <span class="synConstant">1</span> <span class="synConstant">7</span>
Processing: <span class="synConstant">18</span> <span class="synConstant">110</span> <span class="synConstant">7</span>.<span class="synConstant">5</span> <span class="synConstant">111</span> <span class="synConstant">126</span>
Waiting: <span class="synConstant">18</span> <span class="synConstant">110</span> <span class="synConstant">7</span>.<span class="synConstant">5</span> <span class="synConstant">111</span> <span class="synConstant">126</span>
Total: <span class="synConstant">24</span> <span class="synConstant">111</span> <span class="synConstant">7</span>.<span class="synConstant"></span> <span class="synConstant">112</span> <span class="synConstant">127</span>
Percentage of the requests served within a certain <span class="synStatement">time</span> <span class="synStatement">(</span>ms<span class="synStatement">)</span>
<span class="synConstant">50</span>% <span class="synConstant">112</span>
<span class="synConstant">66</span>% <span class="synConstant">112</span>
<span class="synConstant">75</span>% <span class="synConstant">112</span>
<span class="synConstant">80</span>% <span class="synConstant">112</span>
<span class="synConstant">90</span>% <span class="synConstant">113</span>
<span class="synConstant">95</span>% <span class="synConstant">113</span>
<span class="synConstant">98</span>% <span class="synConstant">114</span>
<span class="synConstant">99</span>% <span class="synConstant">114</span>
<span class="synConstant">100</span>% <span class="synConstant">127</span> <span class="synStatement">(</span>longest request<span class="synStatement">)</span>
</pre>
<p>
1秒あたりの処理数はプロキシサーバをかまさないほうが早いのだが。。。同じルーターにぶら下がっているから、ネットワークの影響殆ど無いから
</p>
</div>