nginxでロードバランサー。httpとhttpsの両方で使う

仕事でhttpとhttps両方使う必要がでてきた。

サーバー構成は複数のアプリサーバーをロードバランサーで処理振り分けをする、よくあるタイプ。

問題はhttpsで、複数あるアプリサーバーにそれぞれSSL証明書をインストールすると、手間もかかるがお金もかかるので控えたい。

そこで、サーバー1台をロードバランサーとして仕立て上げ、そのサーバーにだけSSL証明書をインストールし、処理の振り分け先のアプリサーバーはすべてhttpで処理するといいんじゃないか、と考えた。

apacheでも同じこと出来ると思うが、軽量高速と評判
のnginxを使う。

なんと言っても、設定の単純さが際立つ。

nginx.conf 設定例

user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    upstream tests {
      server 127.0.0.1:8000;
      server 127.0.0.1:8001;
      server 127.0.0.1:8002;
    }

    server {
        listen       80;
        server_name  _;
        location / {
            proxy_pass http://tests;
            index  index.html index.htm;
        }
    }

    server {
        listen       443;
        server_name  _;
        ssl                  on;
        ssl_certificate      /etc/pki/tls/certs/localhost.crt;
        ssl_certificate_key  /etc/pki/tls/private/localhost.key;
        #    ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
        location / {
            proxy_pass http://tests;
            index  index.html index.htm;
        }
    }
}

以上で設定おわり。
あとは振り分け先ががんばればよい。

設定のキモは、upstream セクションの振り分け設定に、server セクション内location ディレクティブで proxy_pass で向けているところ。

SSL用の server セクションも、listenが443なのと、ssl系の設定が追加してあるだけで、location内でporxy_pass を設定しているのは同じ。

超簡単。
特に設定しなくても、upstream内の振り分けメンバーが落ちたらちゃんと振り分け先から外してくれるし、復帰したら振り分け先に戻してくれる。
感動した。

nginx公式サイトの設定例:
http://wiki.nginx.org/NginxLoadBalanceExample

重み付けもできる
http://wiki.nginx.org/NginxHttpUpstreamModule#upstream

      • -

追記
abコマンドでたたいてみたら、http側でのテストではnginxのCPU負荷は5%くらいでとっても余裕、https側のテストではnginxのCPU負荷90%とか行った。
SSLすげえ重いな。

nginx側はDTIのServersMan@VPS、ab側は自宅のノートPCのVMWarePlayerだから、あんまり結果に意味ないと思うけど、abテストの結果をのっけておく。

httpへのabテスト
# ab -n 1000 -c 100 http://*********/
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/

Server Software:        nginx/0.8.55
Server Hostname:        **************
Server Port:            80

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      100
Time taken for tests:   9.370 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      258000 bytes
HTML transferred:       5000 bytes
Requests per second:    106.72 [#/sec] (mean)
Time per request:       937.032 [ms] (mean)
Time per request:       9.370 [ms] (mean, across all concurrent requests)
Transfer rate:          26.89 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       11  252 789.6     47    4436
Processing:    21  270 834.5     54    9021
Waiting:       21  263 832.8     52    9021
Total:         36  522 1303.5    102    9042

Percentage of the requests served within a certain time (ms)
  50%    102
  66%    115
  75%    125
  80%    132
  90%   3069
  95%   3161
  98%   6139
  99%   6151
 100%   9042 (longest request)
httpsへのテスト
# ab -n 1000 -c 100 https://*********/
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/

Server Software:        nginx/0.8.55
Server Hostname:        **************
Server Port:            443
SSL/TLS Protocol:       TLSv1/SSLv3,DHE-RSA-AES256-SHA,1024,256

Document Path:          /
Document Length:        5 bytes

Concurrency Level:      100
Time taken for tests:   25.925 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      258000 bytes
HTML transferred:       5000 bytes
Requests per second:    38.57 [#/sec] (mean)
Time per request:       2592.517 [ms] (mean)
Time per request:       25.925 [ms] (mean, across all concurrent requests)
Transfer rate:          9.72 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      351 2361 825.5   2400    8492
Processing:    21  113  94.1     95     855
Waiting:       20  108  85.3     93     854
Total:        521 2475 813.1   2499    8592

Percentage of the requests served within a certain time (ms)
  50%   2499
  66%   2642
  75%   2789
  80%   2906
  90%   3115
  95%   3291
  98%   4578
  99%   6458
 100%   8592 (longest request)
            • -

追記その2(2012/2/29)

最近のSSLって同じドメインなら何台にインストールしても追加料金なしなのがあるんだねー。
なら無理にロードバランサ側でSSL仕込まないほうがいいな。