URLごとに接続するサーバを切り替える(HAProxy)(HTTPS)
以下のように同一IP同一ポートでの接続をドメイン名で切り替える設定をHTTPSで行いたいと思います。
※前回はHTTP(80番ポート)接続での設定で、今回はHTTPS(443)での接続です。
※フロントサーバ(リバースプロキシサーバ)はAlmaLinux9です。
参考サイトは以下です。
https://serverfault.com/questions/236496/haproxy-not-passing-ssl-traffic-in-tcp-mode-unknown-protocol
https://serverfault.com/questions/1029930/having-trouble-with-tcp-mode-on-haproxy-running-on-opnsense
https://www.haproxy.com/documentation/hapee/latest/onepage/
以下のコマンドを実行しHAProxyをインストールします。(既にインストール済みの場合は不要)
dnf -y install haproxy
以下のコマンドを実行し設定ファイルを開きます。(viの使い方は省略します)
vi /etc/haproxy/haproxy.cfg
443で待ち受けするfrontendを作成します。
frontend https_proxy
bind *:443
mode tcp
tcp-request inspect-delay 5s
tcp-request content accept if { req_ssl_hello_type 1 }
use_backend https_servers1 if { req_ssl_sni -i aaa.com }
use_backend https_servers2 if { req_ssl_sni -i bbb.com }
参考
frontend https_proxy ←frontendの名前をhttps_proxyに設定
bind *:443 ←全IPアドレスで443ポートの場合に実行
mode tcp ←tcpモード
tcp-request inspect-delay 5s ←5秒まで遅延を許可。おまじない
tcp-request content accept if { req_ssl_hello_type 1 } ←SSLでは必要
use_backend https_servers1 if { req_ssl_sni -i aaa.com } ←aaa.comの場合はhttps_server1を処理
use_backend https_servers2 if { req_ssl_sni -i bbb.com } ←bbb.comの場合はhttps_server2を処理
次にbackendを作成します。
backend https_servers1
mode tcp
option ssl-hello-chk
option httpclose
server web00 192.168.1.101:443 rise 2 fall 5
backend https_servers2
mode tcp
option ssl-hello-chk
option httpclose
server web00 192.168.1.102:443 rise 2 fall 5
参考
backend https_servers1 ← https_server1という名前のbackendを作成
mode tcp ←tcpモード
option ssl-hello-chk ←サーバーテストにSSLv3クライアントのHelloヘルスチェックを使用する
option httpclose ←HTTPS リクエスト、セッション状態、タイマーのログを有効にする。backendでは意味なし?
server web00 192.168.1.101:443 rise 2 fall 5 ←転送先ローカルIPアドレスとポート番号、ヘルスチェック2回成功で稼働中判断、5回失敗で停止中判断
設定ファイルを保存し、以下のコマンドを実行して、HAProxyを再起動します。
systemctl restart haproxy
以下のコマンドを実行しHAProxyを自動実行に設定します。
systemctl enable haproxy
フロントサーバのローカルIPアドレスで443番ポートを開放します。
ドメインごとにアクセスし異なるWebサイトが表示されれば成功です!
以下のようにHTTPとHTTPSを合わせて記載することも可能です。
frontend http_proxy
use_backend http1_servers if { hdr(host) -m str aaa.com }
use_backend http2_servers if { hdr(host) -m str bbb.com }
bind *:80
frontend https_proxy
bind *:443
mode tcp
tcp-request inspect-delay 5s
tcp-request content accept if { req_ssl_hello_type 1 }
use_backend https_servers1 if { req_ssl_sni -i aaa.jp }
use_backend https_servers2 if { req_ssl_sni -i bbb.jp }
backend https_servers1
mode tcp
option ssl-hello-chk
option httpclose
server web00 192.168.1.101:443 rise 2 fall 5
backend https_servers2
mode tcp
option ssl-hello-chk
option httpclose
server web01 192.168.1.102:443 rise 2 fall 5
backend http1_servers
server web02 192.168.1.101:80
backend http2_servers
server web03 192.168.1.102:80