2020/09/07
こんにちは。セシオスサポートチームです。
今回は Nginx を使って疑似ロードバランサを構築します。
検証で初めてnginxを使ってみたのですが、バランシングの設定やSSLの終端設定などが思いの外簡単にできたのでご紹介したいと思います。
Nginxを使ってSSL終端を行い、任意のサーバーへ負荷分散をおこなう。
nginxのリポジトリファイルを作成します。
# vi /etc/yum.repos.d/nginx.repo
nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
yum インストール
# yum install nginx -y
SSLサーバー証明書を任意のディレクトリへ配置してください。
検証用なので、LetsEncrypt などで用意するのがお手軽かと思います。
ここでは説明は省略します。
標準ファイル無効化
# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disable
nginx設定ファイル作成
# vi /etc/nginx/conf.d/loadbalancer.conf
upstream loadbalancer {
server webapplication01.secioss.com;
server webapplication02.secioss.com;
server webapplication03.secioss.com;
}
server {
listen 80;
listen 443 ssl;
server_name webapplication.secioss.com;
ssl_certificate 証明書のパス;
ssl_certificate_key 証明書の秘密鍵パス;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://loadbalancer;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
upstream という記述で負荷分散を行うサーバーを指定します。
loadbalancer と指定している箇所は好きな名前で構いません。
振り分けの方式については特に指定しなかった場合はラウンドロビン方式にて振り分けを行います。
IP単位で振り分けたい場合は ip_hash を定義します。
upstream loadbalancer {
ip_hash;
server webapplication01.secioss.com;
server webapplication02.secioss.com;
server webapplication03.secioss.com;
}
location ディレクティブ中にある、 proxy_pass の指定は upstream で定義した名前を設定してください。
今回はloadbalancer として定義していますが、その他の名前にする場合は proxy_passも同様に変更する必要があります。
設定を保存したら、テストを実行し、エラーが出ないか確認します。
# nginx -t
成功メッセージ
nginx: configuration file /etc/nginx/nginx.conf test is successful
test is successfulと表示されれば問題ありません。
nginx 起動
# systemctl enable nginx
# systemctl start nginx
以上で構築手順は完了です。
いかがでしょうか?
Nginxは設定の項目名がapahceに寄せてあって、なんとなくわかりやすいですし、
xmlのような形式ではないので、見やすくていいですね。
以上、セシオスサポートチームでした。