LoginSignup
2
3

More than 5 years have passed since last update.

HAproxy,varnish,h2oで環境構築

Last updated at Posted at 2016-11-09

No, Varnish still won't add SSL/TLS support.

varnish5.0.0でもsslの対応はしないらしいのですが、

Instead in Varnish 4.1 we have added support for Willys PROXY protocol which makes it possible to communicate the extra details from a SSL-terminating proxy, such as HAProxy, to Varnish.

HAProxyを使えばそれっぽくはなりますよとのことだったので設定してみました。
varnishといえば、以前からssl非対応で、ssl対応のためにLBとしてnginx等を入れるというやり方が以前からありますが、これはそのnginxの役割をHAProxyでやっているようなものです。

各バージョンの情報

  • HA-Proxy 1.5.4
  • varnish 4.1.3
  • h2o 2.0.4

参考にしたページ

こちらを参考に設定してみて、うまくいかなかったら以下の手順を確認する方が良いかもしれないので先に記載します。
https://blog.feryn.eu/varnish-4-1-haproxy-get-the-real-ip-by-leveraging-proxy-protocol-support/

varnishの設定

/etc/varnish/default.vclの編集

default.vcl
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    set req.http.x-clientip = client.ip;
    set req.http.x-serverip = server.ip;
    set req.http.x-localip = local.ip;
    set req.http.x-remoteip = remote.ip;
}

backend defaultはh2o側で寄せるため、デフォルトのままにしました。
vcl_recvは何行か追加しています。

varnishの再起動

[test-web01 ~]# /etc/init.d/varnish restart
Stopping Varnish Cache:                                    [  OK  ]
Starting Varnish Cache:                                    [  OK  ]
You have new mail in /var/spool/mail/root
[test-web01 ~]# 

特にエラーが発生しなければOKです。

HAProxyの設定

/etc/haproxy/haproxy.cfgの修正

認証用のサーバー証明書の準備が必要です。

haproxy.cfg
frontend http-in
        bind *:80
                bind *:443 ssl crt /****/****/****/****.pem
        default_backend servers

backend servers
        server server1 127.0.0.1:6081

HAProxyの再起動

特にエラーが発生しなければOKです。
認証周りでwarningが発生することがありますが、errorでなければ一先ず問題ないです。

[test-web01 ~]# /etc/init.d/haproxy restart
Enter PEM pass phrase:
Stopping haproxy:                                          [  OK  ]
Enter PEM pass phrase:
Starting haproxy: Enter PEM pass phrase:
                                                           [  OK  ]
[test-web01 ~]# 

h2oの設定

/etc/h2o/h2o.confの修正

もし、デフォルトのままであれば、localhost:80のportを8080に変更します。
localhost:443の設定があったかと思いますが、varnish側からh2oに対して443でアクセスすることはないので、そのままにするか削除をしてしまって問題ありません。

h2o.conf
user: nobody
hosts:
  "localhost:80":
    listen:
      port: 8080
      host: 0.0.0.0
    paths:
      "/":
        file.dir: /var/www/html
access-log: /var/log/h2o/access.log
error-log: /var/log/h2o/error.log
pid-file: /var/run/h2o/h2o.pid

h2oの再起動

特にエラーが発生しなければOKです。

[test-web01 ~]# /etc/init.d/h2o restart
Stopping h2o:                                              [  OK  ]
Starting h2o: start_server (pid:9908) starting now...
                                                           [  OK  ]
[test-web01 ~]# 

ブラウザで確認

httpでアクセス

httpでアクセス

httpsでアクセス

httpsでアクセス

varnishが動作し、かつhttp,httpsどちらでもアクセスができることが確認できました。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3